1. Overview
systemd is the modern system and service manager in Linux. We use its systemctl command to manage services, and the configurations of these services are kept in unit files.
If we modify the unit file of a service, we need to run the systemctl daemon-reload command before restarting the service so that the new settings can take effect. Otherwise, the new settings aren’t applied. So, we may want to check whether we need to run systemctl daemon-reload.
In this tutorial, we’ll discuss how to determine if we need to run systemctl daemon-reload.
2. Example Setup
We’ll use a simple shell script, daemon_script.sh, in our example:
$ cat daemon_script.sh
#!/bin/bash
while true
do
sleep 5
done
This script calls the sleep 5 command periodically in an infinite while loop. Next, let’s grant the script execute permissions using chmod:
$ chmod u+x daemon_script.sh
Next, let’s take a look at the unit file, daemon_script.service, that we’ll use to run daemon_script.sh as a service using systemd:
$ cat daemon_script.service
[Unit]
Description=Simple Script Service
[Service]
ExecStart=/home/alice/work/daemon-reload/daemon_script.sh
[Install]
WantedBy=multi-user.target
The ExecStart option in the Service section of the unit file specifies the path of our script.
Let’s start our service using systemd:
$ cd /etc/systemd/system
$ sudo ln -s /home/alice/work/daemon-reload/daemon_script.service
$ sudo systemctl enable daemon_script
Created symlink /etc/systemd/system/multi-user.target.wants/daemon_script.service → /home/alice/work/daemon-reload/daemon_script.service.
$ sudo systemctl start daemon_script
After creating a symbolic link to the unit file in the /etc/systemd/system directory, we enable the service using systemctl enable daemon_script. Then, we start the service using the systemctl start daemon_script command. We use these commands together with the sudo command as they require root privileges.
Having started our simple service, let’s check whether it’s running using the systemctl status command:
$ systemctl status daemon_script
● daemon_script.service - Simple Script Service
Loaded: loaded (/etc/systemd/system/daemon_script.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-04-25 11:43:04 EDT; 7s ago
Main PID: 5351 (daemon_script.s)
Tasks: 2 (limit: 4594)
Memory: 536.0K
CPU: 3ms
CGroup: /system.slice/daemon_script.service
├─5351 /bin/bash /home/alice/work/daemon-reload/daemon_script.sh
└─5364 sleep 5
Apr 25 12:43:04 ubuntu-2204 systemd[1]: Started Simple Script Service.
Our service is up and running, as expected.
3. Using systemctl status
We can use the systemctl status command to check if we need to run systemctl daemon-reload for a service. systemctl status shows the runtime status information of the unit files passed to it as arguments.
Let’s use the touch command to change the access and modification time of the unit file:
$ ls -l daemon_script.service
-rw-r--r-- 1 alice alice 149 Apr 25 12:40 daemon_script.service
$ touch daemon_script.service
$ ls -l daemon_script.service
-rw-r--r-- 1 alice alice 149 Apr 25 12:49 daemon_script.service
As is apparent from the output of the last ls -l daemon_script.service command, the unit file has a new timestamp, Apr 25 12:49, after running the touch daemon_script.service command. However, there isn’t any change in the content of the unit file.
Let’s check the status of the service again using systemctl status:
$ systemctl status daemon_script
Warning: The unit file, source configuration file or drop-ins of daemon_script.service changed on disk. Run 'systemctl daemon-reload' to reload units.
● daemon_script.service - Simple Script Service
Loaded: loaded (/etc/systemd/system/daemon_script.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-04-25 11:43:04 EDT; 7s ago
Main PID: 5351 (daemon_script.s)
Tasks: 2 (limit: 4594)
Memory: 536.0K
CPU: 3ms
CGroup: /system.slice/daemon_script.service
├─5351 /bin/bash /home/alice/work/daemon-reload/daemon_script.sh
└─5364 sleep 5
Apr 25 12:43:04 ubuntu-2204 systemd[1]: Started Simple Script Service.
Now, there’s a warning in the first line of the output of systemctl status daemon_script. The message warns us about the change of the unit file and tells us that we should run the systemctl daemon-reload command to reload the unit.
4. Using systemctl show
Another alternative for checking whether we need to run systemctl daemon-reload for a service is to use the systemctl show command:
$ systemctl show daemon_script --property=NeedDaemonReload
NeedDaemonReload=yes
The systemctl show command shows the properties of the units passed to it as arguments. The output list is long if we print all of the properties of a unit. However, we can print a specific property using the —**property option. The NeedDaemonReload property indicates whether the unit file has changed since the configuration was read. Its value is yes in our case, which means that we need to run systemctl daemon-reload.
Let’s check the value of the NeedDaemonReload property after running systemctl daemon-reload:
$ sudo systemctl daemon-reload
$ systemctl show daemon_script --property=NeedDaemonReload
NeedDaemonReload=no
Now, the value of the NeedDaemonReload property is no, as we expected. Additionally, let’s verify that there’s no need to run systemctl daemon-reload using systemctl status:
$ systemctl status daemon_script
● daemon_script.service - Simple Script Service
Loaded: loaded (/etc/systemd/system/daemon_script.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-04-25 11:43:04 EDT; 7s ago
Main PID: 5351 (daemon_script.s)
Tasks: 2 (limit: 4594)
Memory: 1.7M
CPU: 3.663s
CGroup: /system.slice/daemon_script.service
├─5351 /bin/bash /home/alice/work/daemon-reload/daemon_script.sh
└─5364 sleep 5
Apr 25 12:43:04 ubuntu-2204 systemd[1]: Started Simple Script Service.
Notably, as expected, there isn’t any warning message in the output of systemctl daemon-reload.
5. Iterating Over All Services
We checked the status of the daemon_script service using systemctl status and systemctl show after we updated the corresponding unit file using touch. However, we must check all units in the system to determine whether we need to run systemctl daemon-reload. We can do this by iterating over all services in a for loop.
Let’s write a script, check_for_daemon_reload.sh, for this purpose:
$ cat check_for_daemon_reload.sh
#!/bin/bash
service_list=$(systemctl --type=service --state=running | awk '{print $1, $4}' | grep running | awk '{print $1}')
for s in $service_list
do
systemctl show $s --property=NeedDaemonReload | grep yes >& /dev/null
if [ $? -eq 0 ]
then
echo $s
fi
done
We get the list of services running using service_list=$(systemctl –type=service –state=running | awk ‘{print $1, $4}’ | g**rep running | awk ‘{print $1}’). Then, we iterate over the service list in the for loop.
We check for the existence of NeedDaemonReload=yes using systemctl show $s –property=NeedDaemonReload | grep yes >& /dev/null. If the exit status is 0 (the pattern matches the output), we print the name of the service using echo $s within the if statement.
Let’s execute the script after running touch daemon_script.service and granting the script execute permissions via chmod:
$ touch daemon_script.service
$ chmod u+x check_for_daemon_reload.sh
$ ./check_for_daemon_reload.sh
daemon_script.service
Obviously, the only service whose unit file has been modified is daemon_script, as expected.
Although systemctl status generates a human-readable output, systemctl show generates a computer-parsable output. So, we prefer to use systemctl show instead of systemctl status within the script.
6. Conclusion
In this article, we discussed how to determine if we need to run systemctl daemon-reload. We learned that we can use systemctl status for this purpose. Then, we saw that systemctl show displays the properties of services, and the NeedDaemonReload property indicates whether we need to run systemctl daemon-reload for a service when the corresponding unit file is modified. Finally, we examined a script that checks all the services running and determines the need for running systemctl daemon-reload.