-1

I have 5 services running in ubuntu. I check their status daily so have to type in the sudo systemctl status <> command for all the 5 services. So I thought of creating a simple script service_status.sh which will list down the status. It looks like below:

sudo systemctl status receiver0.service
sleep 2
sudo systemctl status receiver1.service
sleep 2
sudo systemctl status receiver2.service
sleep 2
sudo systemctl status sender.service
sleep 2
sudo systemctl status health.service

This runs fine and shows the status of services but the problem is if any of these services is in inactive dead or restarting then the script freezes their and doesn't show the status of the remaining services. For ex, if receiver2.service is not running for some reason:

● receiver2.service -  receiver 2
   Loaded: loaded (/etc/systemd/system/receiver2.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Wed 2021-05-19 07:15:34 EAT; 25s ago
  Process: 5333 ExecStart=/usr/bin/python3 /home/andrew/Documents/source/receiver_
 Main PID: 5333 (code=exited, status=1/FAILURE)
lines 1-5/5 (END)

script stops at this and doesn't show the status of sender.service and health.service. What changes I can make in script so that it doesnt freezes and shows the status of other services as well?

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • it's probably returning an error and bash probably returns that error. You should store the return value in a variable and then you could either handle it, ignore it, or do one of [these solutions](https://stackoverflow.com/questions/11231937/bash-ignoring-error-for-a-particular-command) – wxz May 19 '21 at 04:51

1 Answers1

0

Use is-active and check the return code of the command.

Javier Galarza
  • 28
  • 1
  • 2
  • 5