Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

Bash script to kill all running PHP scripts?

?
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

how do you identify that a PHP script is running?

You may try this:

kill -9 `ps -ef | grep -v grep | greo -i php | awk '{ print $2 }'`
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Gerwin for caching the typo
One more thing is that you need to be root to kill processes owned by other users, otherwise you can kill processes owned by you only.
Avatar of Mark
Mark

ASKER

When I run:

ps -ef | grep -v grep | grep -i php

I am getting not only PHP scripts, but cronjobs which forked the PHP scripts ...

PHP scripts on my system are started as "php thescript.php" so I need to kill lines starting with "php " when running ps -ef
Can you try;

ps -ef | grep -v grep | grep -i "php "
Avatar of Mark

ASKER

ps -ef | grep -v grep | grep -i "php ":

Outputs running processes containing PHP, need a list of processes that begin with "php "
please give example
>> Outputs running processes containing PHP, need a list of processes that begin with "php "
Show us a list of what you're getting, so we can filter and help you further.

ps -ef | grep -v grep | grep -i " php"

The above would grep all processes in your list that start with php when the list looks like this:

rpc        588     1  0 09:57 ?        00:00:00 php thescript1.php
rpcuser   9180     1  0 16:45 ?        00:00:00 php thescript2.php
root      9477     1  0 16:45 ?        00:00:00 php thescript3.php
root      9515     1  0 16:45 ?        00:00:00 php thescript4.php

Open in new window


But if your 'php' would be '/some/path/to/php' the above command would not work, so an example is needed from you.
Avatar of Mark

ASKER

It works, thanks
Avatar of Mark

ASKER

i meant his answer:
kill -9 $(ps -ef | grep -v grep | grep -i php | awk '{ print $2 }')
Mark, that last comment cannot work because in ID: 40514928 you say:

ps -ef | grep -v grep | grep -i php

I am getting not only PHP scripts, but cronjobs which forked the PHP scripts ...

So when you were getting too many lines (including cronjobs), I suggested ID: 40529563 which I tested (working) and you've accepted as answer. Now you are changing your mind of a request of attention? I do not agree.
Avatar of Mark

ASKER

In my case, it is not an issue killing the conjobs as well, I simply selected the wrong answer.
Mark, please look at the answers being suggested and you can choose the right answer from them and this could be multiple answers where you can distribute the points over the correct answers.
Avatar of Mark

ASKER

I thought it originally an issue but it is not