0

I have a php script written, and I want run it as a service/daemon in linux.

So i can use service myservice start/stop/restart

I am assuming it needs to be in the /etc/init.d/

Yogesh
  • 663
  • 8
  • 17
dslauter
  • 75
  • 1
  • 3
  • 6

1 Answers1

0

You can take reference of any prebuilt startup script under /etc/init.d for eg httpd. For eg you can take a reference of below:

#!/bin/bash
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
. /etc/init.d/functions
# Start the service FOO
start() {
       initlog -c "echo -n Starting FOO server: "
    /path/to/FOO &
    ### Create the lock file ###
    touch /var/lock/subsys/FOO
    success $"FOO server startup"
    echo
}
# Restart the service FOO
stop() {
    initlog -c "echo -n Stopping FOO server: "
    killproc FOO
    ### Now, delete the lock file ###
    rm -f /var/lock/subsys/FOO
    echo
}
### main logic ###
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
 status)
    status FOO
        ;;
 restart|reload|condrestart)
    stop
    start
    ;;
 *)
    echo $"Usage: $0 {start|stop|restart|reload|status}"
    exit 1
esac
exit 0

You can have detailed look here

Yogesh
  • 663
  • 8
  • 17
  • In the path to section, won't I have to add something because it is a php file? – dslauter Dec 26 '13 at 20:52
  • You can tweak a little this `bash` script, `/path/to/FOO &` you can run your script and check for its $?, if it is healthy enough it should be started or failed if not healthy. – Yogesh Dec 26 '13 at 20:56
  • take a look [here](http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process) – Yogesh Dec 26 '13 at 20:58