Process running? Stop it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process running? Stop it
# 1  
Old 03-01-2011
Question Process running? Stop it

I have this "process keepalive" script:

Code:
#!/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...

Last edited by LukasB; 03-01-2011 at 04:10 PM.. Reason: Typos
# 2  
Old 03-01-2011
Code:
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-01-2011
Whole script

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

Code:
#!/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.

Last edited by LukasB; 03-01-2011 at 04:25 PM..
# 4  
Old 03-01-2011
Quote:
Originally Posted by LukasB
Thank you! Sorry I am a newbie. So, the whole script would be like this?
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.

Code:
#!/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.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-01-2011
MySQL Thanks

Ok, thank you for not just writing code and bye bye Smilie Really thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nohup not give expected output. Non-stop running process

Hello, I am trying to make a bash script, I tested nohup but it did not help me. My code is: ffmpeg -i $input_url -c:v copy -c:a copy -listen 1 -f mpegts http://localhost:port/live/test When I open it in VLC, it starts feeding my screen and I see bitrate values. When I stop watching it,... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Need help in running a script continuously non stop

Hi, I am running a schedular script which will check for a specific time and do the job. I wanted to run this continuously. Meaning even after the if condition is true and it executes the job, it should start running again non stop. I am using below script #!/bin/sh start: while true do... (10 Replies)
Discussion started by: sandeepcm
10 Replies

3. UNIX for Dummies Questions & Answers

Stop the process

I have the following log file running since yesterday and its consuming so much of the disk space. -rw-rw-r-- 1 dev dba 4543237120 Nov 10 09:00 load_run_file1_0.1111091224.lg How do i kill this process. I don't have any idea of stopping this. Any help would be really appreciated. ... (3 Replies)
Discussion started by: bobby1015
3 Replies

4. UNIX for Advanced & Expert Users

How to stop direct running of executable

Dear Sir, I am using CentOS-5.2(64-bit) as an server side OS in a cluster with 32 slaves+1 Master. My question is, after compiling a file with ifort, I am suppose to get a executable(say a.out). I want my users to do ssh slave.local and then do ./a.out But is it possible to restrict... (0 Replies)
Discussion started by: snbanerjee
0 Replies

5. Shell Programming and Scripting

how to stop the process that is running continously

Hi there, I have written a script to check daily process, each script is in a different directory. Now the first process is running fine, when it goes to the next directory the process doesn't executes. cd result/logs ref=month_1888.log echo $ref>> $logfile cd /max/tot/first... (3 Replies)
Discussion started by: NehaKrish
3 Replies

6. Shell Programming and Scripting

How to stop the script which is running in background

Hi I have a script, which i ran in background, can someone please help in stopping this. i gave this command: ksh abc.ksh & this script sends me a mail every 30 seconds. i have deleted the script but still i am getting the mails. can some one please help me stopping dese. ... (3 Replies)
Discussion started by: Prateek007
3 Replies

7. AIX

a process that never stop

Dears all i have an AIX box in which i am facing a problem with a process as below: /usr/dt/bin/dtexec -open 0 -ttprocid and each time i am killing this process with "kill -9" then it run again after a while. any ideas or solutions will be appreciated. (13 Replies)
Discussion started by: TheEngineer
13 Replies

8. Shell Programming and Scripting

How to find the jobs running in background and stop

Hi All, I have requirement. I am running a job every 30mins. before starting the process, i need to check the process, if the process is still running then i need not trigger the process again, if it is not running then trigger the process again. I am using cron to trigger the shell script. Can... (7 Replies)
Discussion started by: srinivas_paluku
7 Replies
Login or Register to Ask a Question