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 Power and shut down computer
1 Single user mode without network
2 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 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

01 #! /bin/sh
02 while true;
03 do
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
10  # Execute fping and save entire output in log file
12  fping -r 0 -g > fping.log 2>&1
14 # Change to whatever works for your network
16  # Filter out the unreachable hosts
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 (for it to work, you may have to install fping first using apt-get ). Remember to make it executable with

sudo chmod 700

so that systemd can start the program. Then, save the unit file netshow.service (Listing 7) in /etc/systemd/system .

Listing 7


01 [Unit]
02 Description=Listing of active hosts
03 Documentation=man:fping(8)
05 [Service]
06 ExecStart=/usr/sbin/
07 IgnoreSIGPIPE=false
09 [Install]

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 .

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

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.