Greetings Readers! Welcome to an interesting article on Linux daemon/Linux service.
Let’s Begin to Create a Linux Service
The idea behind writing this article is not only to tell you the steps for writing a service. Instead, we thought to present you with a special script that can run on any Linux platform or any Linux distribution.
What is a Service in Linux?
A Linux service is a process that constantly runs in the background. You can though control it using a shell script which can alter the state of the service on demand. There are usually four states defined for a service i.e. start, stop, reload, and restart. And are set from the terminal either by <root> or <sudo> users.
Note: During this article, we’ll be using the words ‘service’ and ‘daemon’ at different times, but both mean the same.
Simple Steps to Create the Service
You only need to follow a few steps to create the Linux service. We have streamlined them for you here.
Prepare the Service Template
Let’s assume you have an application to monitor the no of users logged in and logged out. And you need this app to run uninterrupted in the background. The best approach to achieve this is by creating a Linux service. For this, go to the correct directory path and create the script either by root or <sudo> privileged user.
cd /etc/init.d/ vim linuxsvc
Check the Basic Structure
Where <linuxsvc> is the name of your Linux service. Now let’s look at its basic structure.
#! /bin/sh case "$1" in start) start ;; stop) stop ;; status) status ;; force-reload|restart) stop start ;; *) echo "Usage: $0 {start|stop|restart|force-reload|status}" exit 1 ;; esac
Update Service Script
You now need to copy and paste the below code snippet into your service shell script i.e. </etc/init.d/linuxsvc>. We are assuming that the user’s monitoring app is available at </usr/local/bin/linuxsvc>. Also, we choose the monitoring app name similar to <linuxsvc>. In case you want a different name, change it accordingly.
Full Working Code
#!/bin/sh ### Root user check if [ `id -u` -ne 0 ] then echo "$0 need to run as root or sudo" exit fi # daemon files USER_MONITOR_FILE=/usr/local/bin/linuxsvc # if user application is missing, quit now [ -f $USER_MONITOR_FILE ] || exit 0 # check OS type if [ -e /etc/rc.d/init.d/functions ]; then USER_OS="RedHat" elif [ -e /etc/rc.status ]; then USER_OS="SUSE" elif [ -e /etc/debian_version ]; then USER_OS="Debian" else USER_OS="Unknown" fi # Any Linux Platform support implementation is here case $USER_OS in SUSE) . /etc/rc.status rc_reset ;; LSB) . /lib/lsb/init-functions ;; RedHat) . /etc/rc.d/init.d/functions ;; esac linux_start_daemon() { echo -n "Starting $2 daemon: " case $USER_OS in SUSE) startproc $1 $3 rc_status -v ;; Debian) start-stop-daemon --start --quiet --exec $1 -- $3 echo "." ;; LSB) start_daemon $1 $3 echo "." ;; RedHat) daemon $1 $3 echo ;; *) pgrep -f $1 >/dev/null 2>&1 if [ $? -ne 0 ]; then $1 $3 fi echo "." ;; esac } linux_stop_daemon() { echo -n "Stopping $2 daemon: " case $USER_OS in SUSE) killproc $1 rc_status -v ;; Debian) start-stop-daemon --stop --quiet --retry 8 --exec $1 echo "." ;; LSB) killproc $1 echo "." ;; RedHat) killproc $1 echo ;; *) pgrep -f $1 >/dev/null 2>&1 if [ $? -eq 0 ]; then pkill -f $1 >/dev/null 2>&1 for SECONDS in 1 2 3 4 5 6 7 8 ; do pgrep -f $1 >/dev/null 2>&1 if [ $? -ne 0 ]; then break fi sleep 1 done pgrep -f $1 >/dev/null 2>&1 if [ $? -eq 0 ]; then pkill -9 -f $1 >/dev/null 2>&1 sleep 1 fi fi echo "." ;; esac } linux_status_daemon() { pgrep -f $1 > /dev/null 2>&1 if [ $? -eq 0 ] then echo "$2 is running." else echo "$2 is NOT running!" fi } start() { linux_start_daemon $USER_MONITOR_FILE "LINUXSVC" "" } stop() { linux_stop_daemon $USER_MONITOR_FILE "LINUXSVC" } status() { linux_status_daemon $USER_MONITOR_FILE "LINUXSVC" } case "$1" in start) start ;; stop) stop ;; status) status ;; force-reload|restart) stop start ;; *) echo "Usage: $0 {start|stop|restart|force-reload|status}" exit 1 ;; esac case $USER_OS in SUSE) rc_exit ;; RedHat) exit $? ;; esac
Start Linux Service
Before you start using the service, please change its permissions as mentioned below.
chmod +x /etc/init.d/linuxsvc -v
Once you are done, then launch your Linux service for the first time.
service linuxsvc start
Before You Leave
Thanks for reading this article. We hope this ultimate Linux script will be useful for you. We host some of the best Shell scripting quizzes, see if you like to play around with them.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.