Registering a systemd service in Red Hat Linux 7

Moving from upstart to systemD? Here's how you can get about doing it on Red Hat Linux (I'm using version 7.1 as of now)

A quick note on systemd

systemd is a software suite for central management and configuration of the linux OS. It consists of server applications (
daemons), run time libraries, development tools, and command line utilities.

Creating the .service file

Create a file in /etc/systemd/system/ named {your-service-name}.service and add the following:



[Unit]

Description=A brief description to identify your service

After=syslog.target

[Service]

ExecStart=/srv/{the-file-to-be-executed}

Type=simple

PIDFile=/var/run/{your-service-name}.pid

[Install]



WantedBy=multi-user.target

 

To start/stop the service

sudo systemctl start {your-service-name}.service

sudo systemctl stop {your-service-name}.service

To check the status of the service:

sudo systemctl status {your-service-name}.service

This will tell you the status of the service as well as show the latest snippet of the application log which is pretty neat!

NOTE
In case the service doesn't daemonize, you might want to enable the service to initiate at the machine startup. To do that, run the following command:

sudo systemctl enable {your-service-name}.service

to prevent the service from running at the machine startup run:

sudo systemctl disable {your-service-name}.service

Sources:

Wikipedia - https://en.wikipedia.org/wiki/Systemd

Ubuntu wiki - https://wiki.ubuntu.com/SystemdForUpstartUsers