LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Awk command giving issue for .service file execution (https://www.linuxquestions.org/questions/linux-server-73/awk-command-giving-issue-for-service-file-execution-4175654598/)

newbie14 05-25-2019 12:36 PM

Awk command giving issue for .service file execution
 
I have centos 7. Thus in the /lib/systemd/system I created a new .service as below.
When I am using awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' this command in it does not execute the command. But when I run from command prompt that works fine.

Quote:

[Unit]
Description=Server 11000 Process Restart Upstart Script
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
User=root
TimeoutStartSec=0
Type=simple
KillMode=process
WorkingDirectory=/usr/local/server11000
ExecStart=/bin/bash /usr/local/serverBash_11000.sh | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' | rotatelogs /usr/local/server11000/log/service.log 30M
Restart=always
RestartSec=2
LimitNOFILE=5555

[Install]
WantedBy=multi-user.target
When I start the service e.g. systemctl start server11000.service it just starts. But when I type systemctl status server11000.service below is the output.

Quote:

Server 11000 Process Restart Upstart Script
Loaded: loaded (/usr/lib/systemd/system/server11000.service; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Sun 2019-05-26 01:31:10 +08; 1s ago
Process: 26515 ExecStart=/bin/bash /usr/local/serverBash_11000.sh | awk { print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); } | rotatelogs /usr/local/server11000/log/service.log 30M (code=exited, status=127)
Main PID: 26515 (code=exited, status=127)
CGroup: /system.slice/server11000.service
└─43576 java -cp .:./jars/* server11000
May 26 01:33:16 localhost.localdomain systemd[1]: Unit server11000.service entered failed state.
May 26 01:33:16 localhost.localdomain systemd[1]: server11000.service failed.

scasey 05-25-2019 12:56 PM

What's in journalctl -xe ?

A systemd status=127 means "unknown command" -- try using absolute paths to all commands and files:
Code:

/bin/bash /usr/local/serverBash_11000.sh | /bin/awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' | /sbin/rotatelogs /usr/local/server11000

michaelk 05-25-2019 01:01 PM

I suggest putting the entire command in a script using absolute paths as suggested, make it executable and call the script from the service file.

scasey 05-25-2019 01:11 PM

Quote:

Originally Posted by michaelk (Post 5998898)
I suggest putting the entire command in a script using absolute paths as suggested, make it executable and call the script from the service file.

Excellent point! I'd forgotten that ExecStart runs only one command.

newbie14 05-25-2019 01:39 PM

Hi Scasey & Michaelk,
Actually currently my serverBash_11000.sh is as below. So your suggestion is as to add the command in this bash ? Or should I create another script and put the command in it and leave this script as it is? Sorry a bit confused here.

Quote:

#!/bin/bash
clear
java -cp ".:./jars/*" server11000

scasey 05-25-2019 01:48 PM

Quote:

Originally Posted by newbie14 (Post 5998907)
Hi Scasey & Michaelk,
Actually currently my serverBash_11000.sh is as below. So your suggestion is as to add the command in this bash ? Or should I create another script and put the command in it and leave this script as it is? Sorry a bit confused here.

Will adding the pipe to /bin/awk, etc. after the java do what you what you want?
(Note: should have the absolute path to clear and java in that script as well)

If not, then create a new script containing the commands you already have and ExecStart that script.

newbie14 05-25-2019 01:59 PM

Hi Scasey,
I notice using the awk piping I do miss some earlier missing example when I start my application it get connection pooling.

May 26, 2019 2:52:03 AM com.zaxxer.hikari.HikariDataSource <init>
INFO: HikariPool-1 - Starting...
May 26, 2019 2:52:04 AM com.zaxxer.hikari.HikariDataSource <init>
INFO: HikariPool-1 - Start completed.

But this is not shown in the logs file. Just to confirm must I create a file called service.log first?

Quote:

Will adding the pipe to /bin/awk, etc. after the java do what you what you want?
(Note: should have the absolute path to clear and java in that script as well)
I am not so clear on this ? What are you trying to confirm on this ?

Quote:

If not, then create a new script containing the commands you already have and ExecStart that script.
Yes in the meantime I have tested already this method seems to be working.

scasey 05-25-2019 05:30 PM

Quote:

Originally Posted by newbie14 (Post 5998914)
Yes in the meantime I have tested already this method seems to be working.

There you go...problem solved!

newbie14 05-25-2019 08:41 PM

Hi Scasey,
Just wonrdering what I did correct and efficient right. I created another .sh file and in it I put this /bin/bash /usr/local/serverBash_11000.sh | /bin/awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' | /sbin/rotatelogs /usr/local/server11000. So automatically this calls the serverBash_11000.sh bash file. You were saying regarding clear I should use the path I dont get you on that or should I remove the clear key word?

scasey 05-25-2019 09:49 PM

Quote:

Originally Posted by newbie14 (Post 5998989)
Hi Scasey,
Just wonrdering what I did correct and efficient right. I created another .sh file and in it I put this
Code:

/bin/bash
/usr/local/serverBash_11000.sh | /bin/awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }' | /sbin/rotatelogs /usr/local/server11000.

So automatically this calls the serverBash_11000.sh bash file. You were saying regarding clear I should use the path I dont get you on that or should I remove the clear key word?

Please use [code]code tags when posting code or output.

You said that serverBash_11000.sh contained a clear command and a java command. All I was saying is that you should use absolute paths to those commands as well. That insures the commands can always be found.

I don't know exactly what you're trying to accomplish, so I don't know if the clear command is appropriate..

newbie14 05-26-2019 01:27 AM

Hi Scasey,
Sorry for me missing on the code tag earlier.

Actually what I am trying to achieve is previously I used to have a java daemon wrapper to run a socket server based on java. Since the wrapper have not been update and has issues on new centos 7, thus I did a research and found this new method using service so I am very new to it and learning . I used to use monit to monitor it incase it goes down and able to email me. Hope I am clearer now.

scasey 05-26-2019 03:50 AM

newbie14: to me, java is coffee :) I’ve given all the help I can here.
Please mark the thread SOLVED if you think it is.

newbie14 05-26-2019 06:00 AM

Hi Sean,
Yes sure I have marked it has been helpful I will put more queries when I face more issues.


All times are GMT -5. The time now is 07:50 AM.