How To : running scripts or application on startup CentOS

Posted by cikul | Posted in Linux | Posted on 21-10-2010-05-2008

0

Simplest way is edit file rc.local located in /etc/rc.d/rc.local and put your command there.

Another way is you create script as service so you can start | stop | restart your applications/scripts easily and add to init start.

First. You can create script to call your application/scripts

#!/bin/sh
 #
 # chkconfig: 345 99 01
 #
 # description: startup program

##############################################
 ## Give name appropriate with your applications/scripts
 ## DAEMON indicate the location where you put the program
 ## AppName shows the program that you want to run
 ## ScriptName shows script name you located at /etc/init.d/
 ##############################################
 DAEMON="/usr/bin/AppName"

# to check the applications
 test -f $DAEMON || exit 0

# The init commands
 case "$1" in
 start)
 echo "Starting AppName..."
 $DAEMON > /dev/null 2>&1 &
 ;;
 stop)
 echo "Stopping AppName..."
 kill -9 `ps -C AppName -o pid --no-headers`
 ;;
 restart)
 echo "Stopping AppName..."
 kill -9 `ps -C AppName -o pid --no-headers`
 echo "Starting AppName..."
 $DAEMON > /dev/null 2>&1 &
 ;;
 *)
 echo "usage: /etc/init.d/ScriptName"
 echo "$0 {start | stop | restart}"
 exit 1
 ;;
 esac

Put The scripts in /etc/init.d/ and change permission to 755

chmod 755 /etc/init.d/ScriptName

Now go to /etc/rc.d/rc5.d/ and create link which leads to a service that we created earlier

cd /etc/rc.d/rc5.d/
ln -s /etc/rc.d/init.d/ScriptName S99ScriptName

Register the script so it will run in startup

chkconfig --add ServiceName
chkconfig ServiceName on

Now your scripts/applications will run automatically when startup.

You can start/stop/restart your service using commands :

service ServiceName start
service ServiceName stop
service ServiceName restart

or you can disable your service to run in startup using command :

chkconfig ServiceName stop

Share and Enjoy

Write a comment

*