System V style init and systemd in practice

Slashdot it! Delicious Share on Facebook Tweet! Digg!

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.

Figure 3: Installing a service.

Additionally, the outcome of the executing service can be found in Figure 4.

Figure 4: The log created by the service.

Buy this article as PDF

Express-Checkout as PDF

Pages: 1

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Systemd as central control for the Linux system

    Some Linux followers see systemd as the best thing since sliced bread – for others, it's the work of the devil. However, it also has the stuff to clear out old trenches and form a unified base for Linux.

  • Innovation in the Linux environment

    Testers constantly encounter new trends in the experimental branches of Linux distributions. I look at who drives Linux forward and how the future is shaping up.

  • What is Upstart?

    Originally created for use in Ubuntu, Upstart is suitable for deployment in all Linux distributions as an alternative to the System-V init.

  • Installing and testing Nextcloud

    Leading ownCloud developers, including the project founder Frank Karlitschek, became dissatisfied with the direction of the project, so they started Nextcloud, a fork of the code and a new company. The goal is to create a better balance among the company, clients, and users. We take a look at how Nextcloud is faring.

  • Use multiple distributions at the same time with Bedrock Linux

    Bedrock transparently combines virtually any number of distributions with different architectures, package management, and init systems under a filesystem.