Universal Linux Service Example Using Shell Script

Greetings Readers! Welcome to an interesting article on Linux daemon/Linux service.

The idea behind writing this article is not only to tell you the steps for writing a Linux service. Instead, we thought to present you a special script which can run on any Linux platform or any Linux distribution.

Writing Your Own Linux Service.

Linux service using shell script.

Linux service using a shell script.

Now we’ll demonstrate how to write a Linux service.  A Linux service is a process which 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 means the same.

1- Preparing the 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

 

Where <linuxsvc> is the name of your Linux service. Now let’s look at the basic structure of Linux service example template.

#! /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

 

2- Finalizing the 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 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.

2.1- Full Linux Service Code Using Shell Script.

#!/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

 

3- Assigning permissions and start service.

Before you start using the service, please do 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

 

Final Word.

Thanks for reading this article. We wish this ultimate Linux script will be useful for you. We do host some of the best Shell scripting quizzes, see if you like to play around with them.

 

Please do leave us with your valuable comments and share this post with your friends.

 

Best,

TechBeamers.