Systemmd Replacement of Upstart on Ubuntu 16.04

This article explains , explains and gives you an example of systemmd system service on ubuntu 16.04

Hello all. Today we will see about Systemmd in Ubuntu 16.04

Previously i used to write upstart for my scripts in server less than 14.04.
Now With Ubuntu 16.04 I prefer to write systemmd services.

I am not gonna explain about  upstart much here.

Simple Upstart Script looks like this


description "My Simple Upstart"
author "BALAJI"
start on [2345]
stop on [016]

exec echo Hello bala `date` >> /var/log/hello.log

Save this file in location   /etc/init/hello.conf

Start the upstart by

   sudo service hello start
and check the log by
cat /var/log/hello.log
Hello bala Thu Feb 23 12:22:25 IST 2017

Now Get into Systemmd Ubuntu 16.04

Step 0:

Writing Systemmd Service is simple , It consists of three parts/blocks. They are

  1.  Unit
  2. Service
  3. Install

# !bin/sh
echo "Hello Bala `date`"

Save this as hello.sh . I saved in my home directory. so  path -> /home/bala/hello.sh

Note: Assuming you are root user else use  sudo in front of all the commands if permission denied .

So simple hello.service will be like this


[Unit]
Description="My Hello Service"

[Service]
ExecStart = /bin/sh /home/bala/hello.sh

[Install]
WantedBy = multi-user.target
Alias = hello.service

Save this as hello.service inside  /lib/systemd/system

Step 1: Enable Hello.service by systemctl

systemctl enable hello.service

Created symlink from /etc/systemd/system/hello.service to /lib/systemd/system/hello.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/hello.service to /lib/systemd/system/hello.service.


Step2 : Start the Service by

  systemctl start hello.service

By default systemmd services logged in `syslog` . So open syslog by

cat /var/log/syslog

systemd[1]: Started "My Hello Service".
Feb 23 13:50:24 bala sh[12585]: Hello Bala Thu Feb 23 13:50:24 IST 2017

Step3: Check The status by

systemctl status hello.service


● hello.service - "My Hello Service"
Loaded: loaded (/lib/systemd/system/hello.service; enabled; vendor preset: enabled)
Active: inactive (dead)

Step 4: Stop Systemmd Service by

  systemctl stop hello.service


That's it . Thanks for reading. Happy coding !!!

Leave a comment

(Note: Comments are moderated)