Adding Startup Service with SysVinit by David Papkin

In this post by David Papkin , will show how to create a dummy service named fake_service.

In this and the following exercise, we will create a simple startup service.

First we will do it for a SysVinit system.
Note that if you are using a systemd-based system everything should still work because of the backwards compatibility layer that all distributions utilize. However, in the next exercise we will do natively for systemd.
If you are on a Debian-based system like Ubuntu, make sure you have installed the sysvinit-utils and chkconfig packages. However, recent versions of Ubuntu no longer package chkconfig; you’ll have to use the update-rc.d utility instead.
First we have to create the service-specific script; you can create one of your own for fun, or to get the procedure down just (as root) create a file named /etc/init.d/fake_service containing the following content:
#!/bin/bash
# fake_service
# Starts up, writes to a dummy file, and exits
#
# chkconfig: 35 69 31
# description: This service doesn’t do anything.
# Source function library
. /etc/sysconfig/fake_service
case “$1” in
start) echo “Running fake_service in start mode…”
touch /var/lock/subsys/fake_service
echo “$0 start at $(date)” >> /var/log/fake_service.log
if [ ${VAR1} = “true” ]
then
echo “VAR1 set to true” >> /var/log/fake_service.log
fi
echo
;;
stop)
echo “Running the fake_service script in stop mode…”
echo “$0 stop at $(date)” >> /var/log/fake_service.log
if [ ${VAR2} = “true” ]
then
echo “VAR2 = true” >> /var/log/fake_service.log
fi
rm -f /var/lock/subsys/fake_service
echo
;;
*)
echo “Usage: fake_service {start | stop}”
exit 1
esac
exit 0
Make the file above executable and give other proper permissions:
$ sudo chmod 755 /etc/init.d/fake_service
LFS201: V 1.0 c Copyright the Linux Foundation 2015. All rights reserved.
2 CHAPTER 4. INIT: SYSTEMV, UPSTART, SYSTEMD
You’ll notice the script includes the file /etc/sysconfig/fake service. (On non-RHEL systems you should change
this to /etc/default/fake_service.) Create it and give it the following contents:
VAR1=”true”
VAR2=”true”
Test to see if the script works properly by running the following commands:
$ sudo service fake_service
$ sudo service fake_service start
$ sudo service fake_service stop
Look at the file named /var/log/fake service.log. What does it contain?
For fun you can add additional modes like restart to the script file; look at other scripts in the directory to get examples of what to do.
Next we will want to have the ability to start fake service whenever the system starts, and stop it when it shuts down.
If you do:
$ sudo chkconfig –list fake_service
you will get an error as it hasn’t been set up yet for this. You can easily do this with:
$ sudo chkconfig –add fake_service
and you can turn it on or off at boot time with
$ sudo chkconfig fake_service on
$ sudo chkconfig fake_service off
To test this completely you’ll have to reboot the system to see if it comes on automatically. You can also try varying the runlevels in which the service is running.

 

Next we will do it for systemd

The analogous procedure is to create (as root) a file directly under /etc/systemd/system or somewhere else in that directory tree; distributions have some varying tastes on this. For example a very minimal file named /etc/systemd/system/fake2.service:
[Unit]
Description=fake2
After=network.target
[Service]
ExecStart=/bin/sh -c ’/bin/echo I am starting the fake2 service ; /bin/sleep 30’
ExecStop=/bin/echo I am stopping the fake2 service
[Install]
WantedBy=multi-user.target
Now there are many things that can go in this unit file. The After=network.target means the service should start only after the network does, while the WantedBy=multi-user.target means it should start when we reach multiple-user mode. This is equivalent to runlevels 2 and 3 in SysVinit. Note graphical.target would correlate with runlevel 5.
Now all we have to do to start, stop and check the service status are to issue the commands:
$ sudo systemctl start fake2.service
$ sudo systemctl status fake2.service
$ sudo systemctl stop fake2.service
If you are fiddling with the unit file while doing this you’ll need to reload things with:
$ sudo systemctl daemon-reload as the system will warn you.
To keep an eye directly on the output you can do:
$ sudo tail -f /var/log/messages
(use /var/log/syslog on Ubuntu) either in background or in another windows while the service is running.
To set things up so the service turns on or off on system boot:
$ sudo systemctl enable fake2.service
$ sudo systemctl disable fake2.service

The original source from https://www.linuxfoundation.org/ is

https://lms.360training.com/scorm/linuxfoundation/LFS201/Lab%204.1.pdf. It is posted here for ease of access for students doing scripting.

Please support Linux by visiting  https://www.linuxfoundation.org/

This concludes this post by David Papkin on how to create a simple startup service

http://davidpapkin.org/

David Papkin favorite movies

Robert Deniro in Taxi Driver

Ava Gardner in Singapore (Flim Noir)

Clarke Gable in China Seas

 

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.