0

I write a shell script and installed it on systemctl to run on startup, everything works fine, however if i run "systemctl status myservice" it shows all my script source code instead only the outputs of my script my script looks similar to this (as a example):

while true
do
   echo "pinging..."
   ping -c 10 google.com
   .... blah blah ....
   .... blah blah ....
done

if i do systemctl status myservice it show my code instead "pinging..." and the ping output, how can i do that systemctl only shows the output instead the code?

Here is my systemctl status and systemctl cat outputs:

$systemctl status checkupserver:

● checkupserver.service - Check Servers Online Service
     Loaded: loaded (/etc/systemd/system/checkupserver.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-09 20:54:21 UTC;
   Main PID: 471544 (bash)
      Tasks: 2 (limit: 9054)
     Memory: 1.2M
     CGroup: /system.slice/checkupserver.service
             ├─ 571033 /bin/checkservers.sh                                                            >
echo "Starting Service..."
while true
do
        echo "pinging server..."
        ping -c 10 serverip
        ... blah blah ...
done
 /bin/checkservers.sh
             └─4785411 /bin/checkservers.sh                                                            >
echo "Starting Service..."
while true
do
        echo "pinging server..."
        ping -c 10 serverip
        ... blah blah ...
done
 /bin/checkservers.sh

$systemctl cat checkupserver:

# /etc/systemd/system/checkupserver.service
[Unit]
Description=Check Servers Online Service

[Service]
User=checker
ExecStart=/bin/checkservers.sh
Restart=always
RestartSec=10
TimeoutStopSec=90
KillMode=process
SyslogIdentifier=CheckServers

[Install]
WantedBy=multi-user.target
josephp88
  • 11
  • 1

1 Answers1

0

You're just receiving the printout of the script because you haven't told it how it can be run from your shell.

So you have two options here.

  1. Change your script so it has the appropriate shebang at the start
#!/bin/bash

while true
do
   echo "pinging..."
   ping -c 10 google.com
   .... blah blah ....
   .... blah blah ....
done

or 2. Tell your service file how to execute the script

# /etc/systemd/system/checkupserver.service
[Unit]
Description=Check Servers Online Service

[Service]
User=checker
ExecStart=/bin/bash -c /bin/checkservers.sh
Restart=always
RestartSec=10
TimeoutStopSec=90
KillMode=process
SyslogIdentifier=CheckServers

[Install]
WantedBy=multi-user.target

Either of the two options will give you the below output:

root@var-som-mx6:/tmp# systemctl status checkupserver.service 
● checkupserver.service - Check Servers Online Service
   Loaded: loaded (/tmp/checkupserver.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 1970-01-05 20:11:38 UTC; 4s ago
 Main PID: 3645 (bash)
   CGroup: /system.slice/checkupserver.service
           ├─3645 /bin/bash -c /tmp/checkservers.sh
           └─3646 ping -c 10 10.7.0.50

Jan 05 20:11:38 var-som-mx6 systemd[1]: Started Check Servers Online Service.
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: pinging...
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: PING 10.7.0.50 (10.7.0.50): 56 data bytes
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=0 ttl=64 time=0.406 ms
Jan 05 20:11:40 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=1 ttl=64 time=0.463 ms
Jan 05 20:11:41 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=2 ttl=64 time=0.369 ms
Jan 05 20:11:42 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=3 ttl=64 time=0.399 ms
Jan 05 20:11:43 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=4 ttl=64 time=0.453 ms
Brendan
  • 908
  • 2
  • 15
  • 30
  • Hi @Brendan, thanks for your reply, unfortunately your solution did not work for me, actually my script has the "#!/bin/bash" line at the start, i put the "bin/bash -c" as you suggested on the service file but still giving me the source code of the script, i think that is because i compiled my script with SHC (shc -f myscript)... is there any way to obtain the correct output with the compiled script? – josephp88 May 21 '22 at 16:59