How to Convert Script to Services in Linux

Mini Bytes: In Linux, shell scripts can be converted to Linux services with very few tutorial steps. In this article we can see how to convert script to services in Linux (CentOS, Fedora and RedHat).

How to convert Linux script as a service. To convert Linux script into a Linux service, you just need basic knowledge about Linux.

Here basic knowledge means, you need to know how to navigate through directories and you need to know how to start, stop and restart the services and also you need to know how to manage your scripts.

Managing the scripts is nothing but a easy task and that involves only some simple queries. If you know how to  run the scripts, then it is enough to go through this tutorial, because in this tutorial, I’m going to show you how to convert Linux script into a service that you can run in your Linux operating system

For this tutorial, I have used in CentOS 6 for the tutorial.

What is Services in Linux:

In all the Linux operating system, services will be stored in the /etc/rc.init.d/ or /etc/init.d folder and from there you can run the services and you can also manage the services from anywhere using the telnet or ssh. This makes linux to be more secure and efficient in managing all the services.

If you want to run a service then you need to run the following command

# /etc/init.d/network start

If you want to stop a service then you need to run the following command

# /etc/init.d/network stop

Services are nothing but a kind of scripts in Linux that will contain a series of commands that was used to start, stop or restart your Linux scripts.

Linux Services contains loops that was used to check a series of commands on there status in Linux.

In the below code you can see the series of functions that will be used to write the script. I will explain each and every loops that was used here, so that you can understand how to convert scripts to services in Linux.

Script (save as myscript.sh):

#! /bin/sh

echo "Service was running successfully" | tee -a /tmp/test.log

Service:

#!/bin/bash
 #
 # description: Test server
 #
 # Get function from functions Linux library
 . /etc/init.d/functions
 # Start the service test
 start() {
#Location of the Script
 /tmp/myscript.sh
 echo "Script was running successfully"
 }
 # Restart the service test
 stop() {
# Use name to kill the process
 pkill myscript
 echo "Process terminated successfully"
 }
 ### main logic ###
 case "$1" in
 start)
 start
 ;;
 stop)
 stop
 ;;
 status)
 status FOO
 ;;
 restart|reload|condrestart)
 stop
 start
 ;;
 *)
 echo $"Usage: $0 {start|stop|restart|reload|status}"
 exit 1
 esac
 exit 0

In the above code you can see start() and stop() function that was used to run the commands you gave in your script.

Start() function contains a command, that will be used to initiate a script and the stop() function will contain a command that will be used to stop the initiated script that you have mentioned in the start() function.

The case is used to call the start and the stop function.

You need to save the script without giving any suffix, like the name “myservice“. After saving it in a location move that service file into the following folder location and run the below command that will give you the access to run the service with elevated privileges.

Folder Location:

/etc/init.d/

Command to give execute permission:

chmod 755 <Service Name>

It’s all done and now you can run your service in your terminal without any issue

You may also like...

Leave a Reply

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