Process running? Stop it

I have this "process keepalive" script:

#!/bin/bash

PIDFILE=/tmp/php.pid
PHPSCRIPT=/home/www/mydomain.com/subdomains/www/parser.php

echo 'Checking php process from PID file'
if [ -f $PIDFILE ] ; then
PID=`cat $PIDFILE`
if ps ax | grep -v grep | grep $PID > /dev/null
then
echo "php process still running - great!!!"
else
echo "php process not running - crap!!!"
rm -f $PIDFILE
php $PHPSCRIPT & echo $! > $PIDFILE&
fi
else
echo "Hey no file - first time maybe?"
php $PHPSCRIPT & echo $! > $PIDFILE&
fi

Now, if the process is running, it echoes "php process still running - great!!!".

I would like to do this: if the process is running, stop it. How to do that?

Thank you for any advice...

read PID < /tmp/php.pid
echo "Killing $PID"
kill "$PID" || echo "Couldn't kill PID"

You should run this script under the same user that ran the PHP script (presumably apache), because the system won't allow you to kill a process belonging to someone else. You can't login as apache of course, but if you configure sudo to allow it, you can sudo -u apache /path/to/script.sh to run it under that effective user.

Also note that just because the process receives the kill signal doesn't mean it'll obey it. You should wait a short time, see if it's quit, and start sending it progressively more severe signals, like QUIT, and as a last resort, KILL.

1 Like

Thank you! Sorry I am a newbie. So, the whole script would be like this?

#!/bin/bash

PIDFILE=/tmp/php.pid
PHPSCRIPT=/home/www/mydomain.com/subdomains/www/parser.php

echo 'Checking php process from PID file'
if [ -f $PIDFILE ] ; then
PID=`cat $PIDFILE`
if ps ax | grep -v grep | grep $PID > /dev/null
then
echo "php process still running - let us kill it!!!"
kill "$PID" || echo "Couldn't kill PID"
fi
fi

And about user - cron runs php file and this php file runs (with shell_exec) the sh file.

Your code isn't that bad actually, there's just a few things to learn which will help simplify what you do. Remember you can use 'exit' to make the script quit at any time, which helps avoid n+1 levels of nested nesting.
Also get in the habit of bracketing and quoting your variables. The quotes prevent things from being split into several arguments when you end up with spaces in filenames and such. And the brackets let you "${put}_${together}${variables}" when "$it_$wont_$work_$without$them": (because _ is a valid char for a variable name, making it look for $it_ instead of $it, etc.

#!/bin/bash

PIDFILE="/tmp/php.pid"
PHPSCRIPT="/home/www/mydomain.com/subdomains/www/parser.php"

echo 'Checking php process from PID file'
# We can split this out to reduce the amount of nesting
if [ ! -f "$PIDFILE" ]
then
        echo "Hey no file - first time maybe?"
        # Don't need the & after $PIDFILE.
        php $PHPSCRIPT & echo $! > "$PIDFILE"
        # not all shells need this, but bash does
        disown
        # Script will quit here.
        exit 0
fi

# We know the PID file must exist to get to this point.

# cat is not needed here.  read and redirection are much more direct and efficient.
read PID < "$PIDFILE"

# Don't need to run ps and grep.
# All running processes will have directories under /proc.
# Things under /proc/ aren't really files and directories, it's a direct view
# of what the kernel believes is going on.
if [ -d "/proc/$PID" ]
then
        echo "php process still running - let us kill it!!!"
        kill "$PID" || echo "Couldn't kill $PID"
else
        echo "php process not running - crap!!!"
        rm -f "$PIDFILE"
        # You don't need the & after $PIDFILE.
        php "$PHPSCRIPT" & echo $! > "$PIDFILE"
        # not all shells need this, but bash does
        disown
fi

Ideally, your PHP script should be responsible for making, and deleting, its own PID file.

1 Like

Ok, thank you for not just writing code and bye bye :slight_smile: Really thanks