LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to launch PHP script at startup on RHEL? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-launch-php-script-at-startup-on-rhel-4175461576/)

sneakyimp 05-11-2013 06:26 PM

How to launch PHP script at startup on RHEL?
 
I've got a PHP script I'd like to launch when my RHEL server boots up.

Should I just add this line to /etc/rc.d/rc.local?
Code:

php -q /path/to/my/script.php > /path/to/output.txt
Or is there some better way?

chrism01 05-11-2013 06:45 PM

That would do it, but note it won't shutdown cleanly, it'll probably just get killed.
If you want a more controlled/nice shutdown, copy one of the other scripts in /etc/init.d and adapt it to start & stop your process.

sneakyimp 05-15-2013 05:32 PM

chrism01, I appreciate your suggestion and am definitely interested in an orderly shut down because my script locks batches of records in a database for processing. Should the server start to shut down, it should unlock any records it has locked so that some other process/machine might work on those records.

I've been looking at the fail2ban init script which looks like it uses bash rather than sh. It's kind of greek to me, though. Any suggestions on where to learn what this stuff means?
Code:

#!/bin/bash
#
# chkconfig: - 92 08
# description: Fail2ban daemon
#              http://fail2ban.sourceforge.net/wiki/index.php/Main_Page
# process name: fail2ban-server
#
#
# Author: Tyler Owen
#

# Source function library.
. /etc/init.d/functions

# Check that the config file exists
[ -f /etc/fail2ban/fail2ban.conf ] || exit 0

FAIL2BAN="/usr/bin/fail2ban-client"

RETVAL=0

getpid() {
    pid=`ps -eo pid,comm | grep fail2ban- | awk '{ print $1 }'`
}

start() {
    echo -n $"Starting fail2ban: "
    getpid
    if [ -z "$pid" ]; then
        $FAIL2BAN -x start > /dev/null
        RETVAL=$?
    fi
    if [ $RETVAL -eq 0 ]; then
        touch /var/lock/subsys/fail2ban
        echo_success
    else
        echo_failure
    fi
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping fail2ban: "
    getpid
    RETVAL=$?
    if [ -n "$pid" ]; then
        $FAIL2BAN stop > /dev/null
    sleep 1
    getpid
    if [ -z "$pid" ]; then
        rm -f /var/lock/subsys/fail2ban
        echo_success
    else
        echo_failure
    fi
    else
        echo_failure
    fi
    echo
    return $RETVAL
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        getpid
        if [ -n "$pid" ]; then
                echo "Fail2ban (pid $pid) is running..."
                $FAIL2BAN status
        else
                RETVAL=1
                echo "Fail2ban is stopped"
        fi
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac

exit $RETVAL


chrism01 05-15-2013 06:55 PM

1. compare that with a few others and you'll see a fairly std structure, then you just put your stuff instead of fail2ban etc
2. have a read of this http://www.tldp.org/HOWTO/HighQualit...OWTO/boot.html


All times are GMT -5. The time now is 01:59 AM.