techfolks linux tutorials
Centos Linux

How to Run a Shell Script as Automatic Start Up Service in Linux Centos


Follow below steps to Create start services at startup automatically in Linux.

Run a Shell Script as Automatic Start Up Service

1. Create a Shell Script Serivce named myserviced(anyname is ok) in /etc/init.d dir

2. Add it to chkconfig

sudo /sbin/chkconfig –add myserviced

3. To check whether the serivce is sucessfully added in chkconfig

sudo /sbin/chkconfig –list myserviced

4. Set it to autostart

sudo /sbin/chkconfig myserviced on

5. Give Permissions to serivce

sudo chmod 775 myserviced

sudo chmod -x myserviced

6. To stop a service from auto starting on boot

sudo /sbin/chkconfig myserviced off

and reboot the server to check.

Sample Code for myserviced service — .sh Shell file

Store this file in /etc/init.d with file name as myserviced and just keep it as only file not as .txt or anything

Sample Code for myserviced service — .sh Shell file

Store this file in /etc/init.d with file name as myserviced and just keep it as only file not as .txt or anything

 

/* Code starts from here set permissions to .sh 'chmod 775 /etc/init.d/myserivced'

#!/bin/sh
# Starts and stops script
#
#
# myserivced Startup script for the shell file
#
# chkconfig: – 95 07
# description:
# .
# processname:

case “$1” in
start)
/* Mention services
/sbin/service httpd start
/sbin/service mysqld start
exec /home/SSI/mytemp/startup.sh

;;

stop)

exec /home/SSI/mytemp/shutdown.sh
;;

restart)
$0 stop
$0 start
;;

status)
exit 1
;;

*)
echo “Usage: $0 {start|stop|restart|status}”
exit 1
esac

code ends here*/