views:

93

answers:

3

I have an init.d script that looks like:

#!/bin/bash
# chkconfig 345 85 60
# description: startup script for swapi
# processname: swapi

LDIR=/var/www/html/private/daemon
EXEC=swapi.php
PIDF=/var/run/swapi.pid
IEXE=/etc/init.d/swapi

### BEGIN INIT INFO
# Provides: swapi
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: startup script for swapi
# Description: startup script for swapi.php which processes actionq into switch
### END INIT INFO

if [ ! -f $LDIR/$EXEC ]
then
        echo "swapi was not found at $LDIR/$EXEC"
        exit
fi

case "$1" in
  start)
        if [ -f $PIDF ]
        then
                echo "swapi is currently running. Killing running process..."
                $IEXE stop
        fi
        $LDIR/$EXEC >> $LDIR/swapi.log & MYPID=$!
        echo $MYPID > $PIDF
        echo "swapi is now running."
        ;;
  stop)
        if [ -f $PIDF ]
        then
                echo "Stopping swapi."
                PID_2=`cat $PIDF`
                if [ ! -z "`ps -f -p $PID_2 | grep -v grep | grep 'swapi'`" ]
                then
                        kill -9 $PID_2
                fi
                rm -f $PIDF
        else
                echo "swapi is not running, cannot stop it. Aborting now..."
        fi
        ;;
  force-reload|restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Use: /etc/init.d/swapi {start|stop|restart|force-reload}"
        exit 1
esac

And then I have a keepalive cronjob that calls this if the pid goes down. Problem is that that keepalive script hangs whenever I run it like a cron job (ie. run-parts /var/www/html/private/fivemin), (the keepalive script being in /var/www/html/private/fivemin).

Is there something funky in my init.d script that I am missing?

I have been racking my brain on this problem for hours now! I am on centos4 btw.

Thanks for any help. -Eric

EDIT:

The keepalive/cronjob script was simplified for testing to a simple:

#!/usr/bin/php
<?

exec("/etc/init.d/swapi start");

?>

The strange this is the error output from swapi.php is put into /var/spool/mail like normal cron output except that I have all the output being dumped into the swapi.log in the init.d script?

When I run keepalive.php from the cli (as root from /) it operates exactly as I would expect it to.

When keepalive runs ps aux | grep php looks like:

root      4525  0.0  0.0  5416  584 ?        S    15:10   0:00 awk -v progname=/var/www/html/private/fivemin/keepalive.php progname {?????   print progname ":\n"?????   progname="";????       }????       { print; }
root      4527  0.7  1.4 65184 14264 ?       S    15:10   0:00 /usr/bin/php /var/www/html/private/daemon/swapi.php

And If I do a:

/etc/init.d/swapi stop

from the cli then both programs are no longer listed.

Swapi ls -l looks like:

-rwxr-xr-x  1 5500 5500 33148 Aug 29 15:07 swapi.php

Here is what the crontab looks like:

*/5 * * * * root run-parts /var/www/html/private/fivemin

Here is the first bit of swapi.php

#!/usr/bin/php
<?
chdir(dirname( __FILE__ ));
include("../../config/db.php");
include("../../config/sql.php");
include("../../config/config.php");
include("config_local.php");
include("../../config/msg.php");

include("../../include/functions.php");

set_time_limit(0);
echo "starting @ ".date("Ymd.Gi")."...\n";
$actionstr  =   "";
while(TRUE){

I modified the init.d script and put init above the variable declarations, it did not make a difference.

A: 

When you run a command from cron, the environment is not the same as when it is run from the bash command line after logging in. I would suspect in this case that the sh is not able to understand swapi.php as a PHP command.

Do a

which php

to see where your php binary is and add this to your init.d script

PHP=/usr/bin/php 
...
$PHP $LDIR/$EXEC >> $LDIR/swapi.log & MYPID=$!

Probably not that important but you may want to redirect the output from the cron line

0 * * * * /path/to/script 2>&1 >> /dev/null

for instance.

ring0
If the php interpreter wouldn't be found, then the script would simply throw an error, and not get stuck.
polemon
The php script will be interpreted by the shell, not by the php interpreter. If the `<` (first char of a php script usually for `<?php`) is interpreted by the shell, and not driven to the php interpreter, it is likely to hang. It may be another word in the php script - but cron is anyway unlikely to call the php interpreter and it is very possible in such a script that a line that woukd be interpreted correctly by PHP, will not be understood the same way by shell, and may cause a hanging of the process.
ring0
Could we see the swapi.php script to have a better idea of what will happen when it is interpreted by shell?
ring0
@ring0: That's why I asked for the first lines of his swapi.php ;)
polemon
Thanks for the suggestions so far, I added all the requested information. Hope that helps!
variable
Did you try with our suggestions?
ring0
@ring0 - Yes I tried running it from the command line, it works fine then. It is starting the swapi program correctly, but then the keepalive script stays open until swapi exits, which since it is daemon, is only when I kill it. I added all of the code above, does any of that help?
variable
A: 

Make sure your script has the correct execution permissions, the right owner, and the first lines should look like this:

#!/usr/bin/php
<?php
polemon
A: 

The answer was that bash was staying open because my init.d script was not redirecting the stderr output. I have now changed it to

$LDIR/$EXEC &> $LDIR/swapi.log & MYPID=$!

And it now functions perfectly.

Thanks for the help everyone!

variable