Autostart application after CentOS reboots

If you only want to run a script after CentOS boots, you could put the command to rc.local file.
vim /etc/rc.d/rc.local
# type in your script at the bottom of this file
If you want to start and shutdown the program along with the system, you might need to create a service using chkconfig. To do so,

1.create a script file at /etc/init.d/
Here is an example of the content of the file
# create file( you need root privilege to do)
cd /etc/init.d/
vim emen2
and add the followings to the file
#!/bin/sh
#chkconfig: 235 80 05
#description: EMEN2 database server
#source function library
EMEN2_HOME=/srv/EMAN2
if [ ! -f $EMEN2_HOME/eman2.bashrc ]
then
  echo "EMEN2 startup: cannot start, missing eman2.bashrc"
  exit
fi
case "$1" in
  "start")
    echo -n "Start emen2 service"
    source $EMEN2_HOME/eman2.bashrc
    # To run it as root
    $EMEN2_HOME/Python/bin/emen2ctl start -h /srv/db_env -e default,site,em --port 8080
    # Or to run it as dedicated user
    /bin/su - username -c $EMEN2_HOME/Python/bin/emen2start
    echo "."
    ;;
  "stop")
    echo - n "Stop emen2 service"
    source $EMEN2_HOME/eman2.bashrc
    # To run it as root
    $EMEN2_HOME/Python/bin/emen2ctl stop -h /srv/db_env -e default,site,em --port 8080
    # Or to run it as dedicated user
    /bin/su - username -c $EMEN2_HOME/Python/bin/emen2stop
    echo "."
    ;;
  *)
    echo "Usage: /sbin/service emen2 {start|stop}"
    exit 1
esac
exit 0

**About the three values after #chkconfig
(1). The first value ’235′ states the default runlevels at which the service should be started. In this case, the service is configured to start at runlevel 2,3 & 5.
(2). The second value ’80′ states the start priority of the service. In this case the service shall be started after starting all those services having start priority less than 80.
(3). The third value ’05′ states the stop priority of the service. In this case the service shall be stopped after stopping all those services having stop priority less than 05.

**/bin/su -c can only accept simple command, which means there should be no "-" or "--" in the command.

2. make the file runnable:
chmod a+x emen2
 
3. add the newly created service to the start up list by
/sbin/chkconfig/emen2 on

No comments:

Post a Comment

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...