I have Centos 7+ systems, and they all use systemd. Sometimes the best way to proceed after an important system change is to restart each currently running service. If I restart each service one-at-a-time, then the system generally stays online, and it is very easy to see service problems as they appear. So I hacked out this little bash script, which restarts each running systemd service, EXCEPT certain named services which I think are core to keeping the box online.
#!/bin/bash
set -e
set -u
running=$(systemctl list-units --type service | grep running \
| grep -iv audit \
| grep -iv disk \
| grep -iv drive \
| grep -iv getty \
| grep -iv irq \
| grep -iv libstoragemgt \
| grep -iv lvm \
| grep -iv multipath \
| grep -iv polkit \
| grep -iv storage \
| cut -d' ' -f1)
for service in $running ; do
echo "$service"
systemctl restart "$service"
done
There are several things I wish were better:
- Grepping for "running" is a crude way to filter for running tasks, and the multiple pipes are pretty expensive.
- Using
cut
is really brittle, and breaks when the output format of systemd changes. systemctl restart
does not set a return code on start failure, so the script keeps going even if a service fails to stop or start.
What may be some better ways to to this?
Reboot the server :)
Restarting all services is rarely a good idea. Either you are updating the whole system and then you want to make sure you that everything uses the latest patched libraries. Or you are updating / reconfiguring just one service and then a simple
systemctl reload whatever.service
should be enough, no need to restart all running services.Besides it's a good idea to reboot the server from time to time to ensure that it actually is rebootable. I have seen servers with years of uptime that no one dares to reboot because no one knows if they will actually come back up, what changes have been made and not saved, etc.
If it is a critical system it is surely in some High-Availability cluster and rebooting one node won't matter. If it is not a critical system you can reboot it, even if after business hours or over the weekend.
So to answer your question - reboot the server
To improve your code you can do the following:
--state running
instead ofgrep running
to filter the services you nees.grep -v -f exclude-services.list
instead of a chain ofgrep
'ssystemctl try-restart s1 s2 s3 ...
all at once instead of looping through the list.First list the services to be excluded in
exclude-services.list
:Then this script should work:
That may be a little more robust than your script. Let me know how it goes.