systemd vs sysvinit part 1

Tags: runlevel, sysvinit, linux
Publish Date: 2016-05-13

For a quick and dirty cheatsheet, see my other post

Differences between systemd and sysvinit (a.k.a init) - Part 1

During your system startup, once your boot-loader (e.g. grub) has finished loading the kernel and ram-disk, it will handover control to a "service manager" to make sure that certain services / daemons are running. systemd and sysvinit are those "service managers".

While sysvinit (or simply init) has been around for a long time, the recent Linux distro's (from Debian 7, RedHat / CentOs 7, Ubntu 15 and CoreOS) have adopted the systemd approach. 

Here in part 1, I will give an overview of how the system starts up and how the services are managed in sysvinit. In part 2, I will give an overview of how this has changed with systemd.


SysVInit - Boot process

In sysvinit, there is a concept of runlevel. A runlevel is a mode in which your system operates, such as 3 = full multiuser-mode, 5 = graphical mode etc.. Just think of it like the "Windows safe mode" being different from the normal mode, where the safe mode will have less features / programs available to you than the normal mode.

You can find the runlevels in /etc/inittab. That is also where you can set a default runlevel. In my case, I see the following line at the bottom of my file:


This indicates that my default runlevel is 3 (Multiuser mode with networking). That would also be the case for most servers.

Every runlevel has its own directory, such as /etc/rc3.d/ (for runlevel 3). If you look at the files in these directories, they all start with either K or S, followed by a number, then by the name of a service, e.g. S64mysqld. Apart from that, they are all symlinks to scripts in /etc/init.d/, where scripts reside to start/stop/restart/reload your services. The numbers in these filenames represent the order in which these scripts get executed. Here is a selection of files (symlinks) that I have in /etc/rc3.d

[root@vagrant-centos64 vagrant]# ls /etc/rc3.d/
lrwxrwxrwx.  1 root root   19 Oct 30  2013 K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx.  1 root root   18 Oct 30  2013 K15svnserve -> ../init.d/svnserve
lrwxrwxrwx.  1 root root   17 Oct 30  2013 K30postfix -> ../init.d/postfix
lrwxrwxrwx.  1 root root   20 Oct 30  2013 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx   1 root root   13 Jan 18  2014 K60nfs -> ../init.d/nfs
lrwxrwxrwx   1 root root   20 Jan 18  2014 K69rpcsvcgssd -> ../init.d/rpcsvcgssd
lrwxrwxrwx   1 root root   20 Jan 18  2014 K87multipathd -> ../init.d/multipathd
lrwxrwxrwx.  1 root root   21 Oct 30  2013 K87restorecond -> ../init.d/restorecond
lrwxrwxrwx.  1 root root   15 Oct 30  2013 K89rdisc -> ../init.d/rdisc
lrwxrwxrwx   1 root root   22 Jan 18  2014 S02lvm2-monitor -> ../init.d/lvm2-monitor
lrwxrwxrwx   1 root root   19 Jan 18  2014 S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx   1 root root   18 Jan 18  2014 S08iptables -> ../init.d/iptables
lrwxrwxrwx   1 root root   17 Jan 18  2014 S10network -> ../init.d/network
lrwxrwxrwx   1 root root   17 Jan 18  2014 S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx   1 root root   19 Jan 18  2014 S55memcached -> ../init.d/memcached
lrwxrwxrwx   1 root root   14 Jan 18  2014 S55sshd -> ../init.d/sshd
lrwxrwxrwx   1 root root   16 Jan 18  2014 S64mysqld -> ../init.d/mysqld

When your system boots up, the first process (pid 1) is /sbin/init, then it finds the default runlevel in /etc/inittab. Based on that runlevel number, it navigates to the runlevel directory for that number (/etc/rc3.d/ in my case). Once there, it runs the K-scripts with a stop option and then run the S-scripts with a start option. These scripts are run sequentially in the order to which they are numbered. Once it has run all the K- and S-scripts, your system is then ready with all processes running that you need within that runlevel.

SysVInit - Enable service at startup

So you might wonder: "How do these symlinks land in the runlevel directories?" and "What if I want to enable a service at startup?"

In sysvinit, that is done by chkconfig. Let's say you have installed nginx and you want to enable it at startup:

[root@vagrant-centos64 vagrant]# chkconfig nginx on
[root@vagrant-centos64 vagrant]# chkconfig | grep nginx
nginx          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

With the first command, we have enabled nginx at startup. With the second command, we check for which runlevels we have enabled nginx at startup. It shows 2,3,4 and 5. This is the default behaviour for chkconfig <service> on when we leave out the --level option.

If we look at our /etc/rc3.d/ directory, we will see that a S-script has been added for nginx, same for the other directories where nginx has been enabled. However, if we look at the runlevel directories 0,6, for which nginx has been disabled (see output chkconfig above), we will see that a K-script has been created for nginx.

[root@vagrant-centos64 vagrant]# ls -l /etc/rc3.d/ | grep nginx
lrwxrwxrwx  1 root root 15 May 12 11:30 S85nginx -> ../init.d/nginx
[root@vagrant-centos64 vagrant]# ls -l /etc/rc0.d/ | grep nginx
lrwxrwxrwx  1 root root 15 May 12 10:51 K15nginx -> ../init.d/nginx

SysVInit - service command

Please bear in mind that chkconfig is only meant to make sure that certain services are started and stopped when you change into another runlevel, such as when you boot up your system or by telinit. chkconfig does not start a service or stop a service for you on the spot. For that, you need the service command:

[root@vagrant-centos64 vagrant]# service nginx start
Starting nginx:                                            [  OK  ]

SysVInit - Change runlevels

You would hardly ever change the runlevel while your system is running. But as a system administrator, there might be times that you want to switch to the single-user mode (runlevel 1) to perform some administrative tasks. You can change runlevel through the init command (which has pid 1) or through telinit, which tells init to change runlevel. To find the current runlevel, you can use the runlevel command or who -r.

[root@vagrant-centos64 vagrant]# telinit 1
[root@vagrant-centos64 vagrant]# who -r
         run-level 1  2016-05-13 08:36                   last=3

Quick and dirty systemvinit vs systemd vs upstart cheatsheet

Tags: linux, upstart, runlevel, systemctl, service, sysvinit, systemd
Publish Date: 2016-05-13

Quick and dirty cheatsheet





Start a service

service <name> start

systemctl start <name>

service <name> start

Stop a service

service <name> stop

systemctl stop <name>

service <name> stop

Enable service at startup

chkconfig <name> on


chkconfig <name> --level 2,3

systemctl enable <name>

update-rc.d <name> enable


update-rc.d <name> enable 2 3



Disable service at startup

chkconfig <name> off

systemctl disable <name>

update-rc.d <name> disable

Set default runlevel

Edit in /etc/inittab



systemctl set-default <>

Edit in /etc/init/rc-sysinit.conf



Get default runlevel

Read from /etc/inittab



systemctl get-default

Read from


Set current runlevel

init <number>

telinit <number>

systemctl isolate <>

init <number>

telinit <number>

Get current runlevel


who -r


who -r


who -r

Compose a hash using inject method

Tags: ruby, hash, inject, enumerable
Publish Date: 2016-04-26

inject is a method defined in the Enumerable module, that most people would use to sum up values.
Here is a classic way people would use this method, whereby the argument 0 is used as a starting value:

[1,2,3,4,5,6,7,8,9,10].inject(0){ |memo, elem| memo + elem }
# => 55

Since inject is defined in Enumerable, it is also available to instances of Hash

# => [Enumerable, Kernel]
{}.respond_to? :inject
# => true

Let's say we have 3 employees, their names and ages are represented this way:

employees = [{name: "Alan", age: 30}, 
             {name: "Tom", age: 45}, 
             {name: "Steve", age: 22} ]

Obviously we could sum up their ages using inject, but this is not necessarily useful

total_age = employees.inject(0) do |memo, elem|
  memo += elem[:age]
# => 97

However, inject is a lot more versatile than that. My former colleague Paul (archan937) likes to use inject as a way to compose a hash, a trick which I have adapted.

In our example, we could use the previous employees array to provide some much more meaningful information, such as the amount of years before each employee reaches his retirement age.

Assuming the retirement age is 65:

years_to_retirement = employees.inject({}) do |memo, elem|
  memo[elem[:name]] = 65 - elem[:age]
# => {"Alan"=>35, "Tom"=>20, "Steve"=>43} 

In the previous example, an empty hash ({}) was provided to inject as the initial value.
Then during each iteration, we added a key-value pair to this hash, using the employee's name as key and years-to-retirement as value.


Pay attention to the second block-parameter

One thing to bear in mind when iterating through a collection with inject (as with other iterators), is that iterating through an array works differently from iterating through a hash.
In the previous examples, we saw that the second block-parameter represents individual elements in the given array. (As expected)

To clarify:

# => Array 
employees.inject(0) do |memo, elem|
  puts elem.inspect
# It prints:
# {:name=>"Alan", :age=>30}
# {:name=>"Tom", :age=>45}
# {:name=>"Steve", :age=>22}

But when iterating through key-value pairs within a hash, the second block-parameter would act differently.
Let's say, my wallet is represented by a hash and the banknotes' denominations (50, 20, 10 & 5) act as keys while the amounts of these banknotes act as the value:

my_wallet = {50 => 1, 20 => 2, 10 => 3, 5 => 2 }

# Now look what happens when we inspect the second block-parameter (called 'elem' in this example):

my_wallet.inject(0) do |memo, elem|
  puts elem.inspect
# It prints:
# [50, 1]
# [20, 2]
# [10, 3]
# [5, 2]

So we saw that inject turns the key-value pairs into arrays of two elements, then pass it on as the second block-parameter.
This behaviour is not unique to inject, but it also applies to other iterators such as map and each.

While we could sum up the amount of money in my_wallet by summing products of elem[0] * elem[1], a much clearer way would be to modify our second block-parameter this way:

my_wallet.inject(0) do |memo, (key,value)|
  memo +=  key * value
# => 130