linuxbashservicecentoscentos6.5

How to create a service on linux Centos 6


How to create service on centos 6 to kill service and run script

For example :

If i run this command service test start

it should make the below

pkill test
nohup ./opt/test/test/bin/standalone.sh -c standalone-full.xml

Also if i run this command service test stop

it should make the below

pkill test


Solution

  • Create a file like /etc/init.d/test

    #!/bin/bash
    
    start() {
        /usr/bin/kill -9 $(cat /tmp/test_nohup_pid.txt)
        nohup ./opt/test/test/bin/standalone.sh -c standalone-full.xml 2>&1 &
        echo $! > /tmp/test_nohup_pid.txt
    }
    
    stop() {
        /usr/bin/kill -9 $(cat /tmp/test_nohup_pid.txt)
    }
    
    restart() {
        stop
        start
    }
    
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      restart)
            restart
            ;;
      *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
    esac