mardi 28 juin 2016

Start a program when os start up Centos 7


When you have a server, this is good that a program is up at boot time. In order to do this, you should have a script to start the application/server in the /etc/init.d/ folder 

To create a script of your own, best is to copy some existing files and adapt it. The main idea is that the script should allow to start, stop and give the status of the application

For instance, lets consider a program starting a apache-ds LDAP. 


!/bin/bash
# Source function library.
. /etc/rc.d/init.d/functions
# Can specify the run levels on the chkconfig line where the program is activated here the program is activated at level 3, 4 and 5 
# Where the run level are the standard linux 3 is multi user, 4 is rarely used, 5 multi user with graphic.
# Then 99 is the start priority (99 is the least, in that case  Apache LDAP will be started after all other program has been started. 
# Then 99 is the stop priority (01 is the highest priority, in that case  Apache LDAP will be stopped the first . 
# chkconfig: 345 99 01
# description:  LDAP startup script
 
start() {
runuser -  ldap -c "cd /home/ldap/apacheds-2/;bin/apacheds.sh default start"
}
 
stop() {
runuser -  ldap -c "cd /home/ldap/apacheds-2/;bin/apacheds.sh default stop"
}
 
 
status() {
runuser -  ldap -c "cd /home/ldap/apacheds-2/;bin/apacheds.sh default status"
}
 
 
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        ;;
  *)
        echo $"Usage: $0 {start|stop|status}"
        exit 2
esac
 
exit $?

Once you have created this script, you should add it to the list of service 
 
# chkconfig --add apacheds

It will create some files in /etc/rc3.d, /etc/rc4.d, /etc/rc5.d

To check this you can pass the command. 
 # chkconfig --list apacheds
 
apacheds        0:off   1:off   2:off   3:on    4:on    5:on    6:off


Aucun commentaire: