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.

ehiller
  • 1,346
  • 17
  • 32
  • Please show use the cron job entry, as well as the header of swapi.php and the permissions it has (`ls -l` in directory of swapi.php). – polemon Aug 28 '10 at 07:03
  • Also, try running you'r init script manually from the console a couple of times, you'll usually stumble over the problem there. Just make sure you use exactly the same conditions as it would be if it was called as a cron job (user, permissions, shell, etc). – polemon Aug 28 '10 at 07:08
  • You should try `kill -15` before you do `kill -9`. See other scripts in `/etc/init.d` for examples. It may not make any difference, but you should move your INIT stanza above the variable definitions. If you're doing `ps -p` I don't think you need `grep -v grep` (`grep 'swapi'` may not be necessary, but it's OK as a sanity check). You really need to show your keepalive script and its cron entry. – Dennis Williamson Aug 28 '10 at 15:00

3 Answers3

5

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!

ehiller
  • 1,346
  • 17
  • 32
0

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.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • If the php interpreter wouldn't be found, then the script would simply throw an error, and not get stuck. – polemon Aug 28 '10 at 07:16
  • The php script will be interpreted by the shell, not by the php interpreter. If the `<` (first char of a php script usually for ` – Déjà vu Aug 28 '10 at 07:31
  • Could we see the swapi.php script to have a better idea of what will happen when it is interpreted by shell? – Déjà vu Aug 28 '10 at 07:38
  • @ring0: That's why I asked for the first lines of his swapi.php ;) – polemon Aug 28 '10 at 07:40
  • Thanks for the suggestions so far, I added all the requested information. Hope that helps! – ehiller Aug 29 '10 at 20:59
  • Did you try with our suggestions? – Déjà vu Sep 01 '10 at 16:31
  • @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? – ehiller Sep 19 '10 at 18:32
0

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
  • 4,722
  • 3
  • 37
  • 48