System V style init and systemd in practice
|
Runlevels and Targets
The runlevels continue to exist in the form of corresponding targets. However, they differ by distribution. The entries here originate from a Debian 8, but are more or less the same in Ubuntu. Table 6 contains a list of comparisons.
Table 6
Runlevel Targets
Init | Runlevel | Action |
---|---|---|
0 | poweroff.target | Power and shut down computer |
1 | rescue.target | Single user mode without network |
2 | multi-user.target | Multi-user mode on |
3 | (As above - In Debian 7 corresponds to runlevel 2) | init [2-5] , exit in s or 1 |
4 | (As above) | Multi-user, network, graphical user interface) |
5 | (As above) | Will be output as 5 with who -r |
6 | reboot.target | Restart computer |
Additionally, there are more targets which can more precisely divide the system states. |
Your Own Service (systemd)
After all that theory, I'll now show how to create your own systemd service. The shell script in Listing 6 collects a list of the hosts found on the network at a given interval. It is saved in executable form under /usr/sbin . You will be able to see the results using tail -f /tmp/netlist.txt .
Listing 6
netshow.sh
01 #! /bin/sh 02 while true; 03 do 04 05 echo "List of active network users" > /tmp/netlist 06 echo "------------------------------------" >> /tmp/netlist 07 date +%d.%m.%Y-%H:%M:%S >> /tmp/netlist 08 echo "------------------------------------" >> /tmp/netlist 09 10 # Execute fping and save entire output in log file 11 12 fping -r 0 -g 192.168.0.0/24 > fping.log 2>&1 13 14 # Change 192.168.0.0/24 to whatever works for your network 15 16 # Filter out the unreachable hosts 17 18 cat fping.log | grep "alive" | sort >> /tmp/netlist 19 echo "------------------------------------" >> /tmp/netlist 20 sleep 120 21 done
You should first create the shell script shown in Listing 6 and save it in the /usr/sbin directory as netshow.sh (for it to work, you may have to install fping first using apt-get ). Remember to make it executable with
sudo chmod 700 netshow.sh
so that systemd can start the program. Then, save the unit file netshow.service (Listing 7) in /etc/systemd/system .
Listing 7
netshow.service
01 [Unit] 02 Description=Listing of active hosts 03 Documentation=man:fping(8) 04 05 [Service] 06 ExecStart=/usr/sbin/netshow.sh 07 IgnoreSIGPIPE=false 08 09 [Install] 10 WantedBy=multi-user.target
Now systemd must be instructed to process the unit file and start the application. To do so, the service must be enabled with sysctl in order to establish a permanent start:
sudo systemctl enable netshow.service
In the process, the required symbolic link will be created in the target directory multi-user.target .
You then start the service with the following command:
sudo systemctl start netshow.service
You can check to see whether the service is running with the following command:
systemctl status netshow.service
You can see the entire installation procedure in Figure 3.
Additionally, the outcome of the executing service can be found in Figure 4.
Buy this article as PDF
Pages: 1
(incl. VAT)