TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Universal Linux Service Example Using Shell Script
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Linux Tutorials

Universal Linux Service Example Using Shell Script

Last updated: Feb 24, 2024 11:10 pm
By Harsh S.
Share
6 Min Read
Linux service using shell script.
Linux service using shell script.
SHARE

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

Contents
Preparing the Service TemplateFinalizing Service ScriptFull Linux Service Code Using Shell ScriptAssign Permissions and Start Our 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.

  • Ultimate Shell Script Quiz for Freshers

Let’s Begin to Create Linux Service

Linux service using shell script.
Linux service using a shell script.

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.

Preparing 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

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

Finalizing 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 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
  • Learn to Set Up / Install MongoDB on Ubuntu

Assign Permissions and Start Our 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

Wrapping Up the Tutorial

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

You Might Also Like

Basic Linux Commands for Beginners With Examples

How to Use Bash to Replace Character in String

Simple Bash Scripting Tutorial for Beginners

20 Bash Script Code Challenges for Beginners with Their Solutions

Difference Between Git Repository and Directory

TAGGED:Shell Script Basics Simplified
Harsh S. Avatar
By Harsh S.
Follow:
Hello, I'm Harsh, I hold a degree in Masters of Computer Applications. I have worked in different IT companies as a development lead on many large-scale projects. My skills include coding in multiple programming languages, application development, unit testing, automation, supporting CI/CD, and doing DevOps. I value Knowledge sharing and want to help others with my tutorials, quizzes, and exercises. I love to read about emerging technologies like AI and Data Science.
Previous Article Configure Static IP on Windows, Ubuntu & OS X Set Static IP on Windows, Ubuntu & OS X
Next Article JNA Tutorial with Java Sample Program How to Write a Simple JNA Program in Java

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x