Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Bash script to kill all running PHP scripts?

?
0
Mark
Asked:
Mark
  • 6
  • 5
  • 4
1 Solution
 
omarfaridCommented:
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 }'`
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
I'd first have a look (minor typo in php grep corrected):

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

This will list the processes that are about to be killed.

and then if ok:

kill -9 $(ps -ef | grep -v grep | grep -i php | awk '{ print $2 }')

Or better if  you know the user is launching the PHP scripts:

ps -u <that_user> | grep -i php

and:

kill -9 $(ps -u <that_user> | grep -i php | awk '{ print $1 }')
0
 
omarfaridCommented:
Thank you Gerwin for caching the typo
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
omarfaridCommented:
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.
0
 
MarkAuthor Commented:
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
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Can you try;

ps -ef | grep -v grep | grep -i "php "
0
 
MarkAuthor Commented:
ps -ef | grep -v grep | grep -i "php ":

Outputs running processes containing PHP, need a list of processes that begin with "php "
0
 
omarfaridCommented:
please give example
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>> 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.
0
 
MarkAuthor Commented:
It works, thanks
0
 
MarkAuthor Commented:
i meant his answer:
kill -9 $(ps -ef | grep -v grep | grep -i php | awk '{ print $2 }')
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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.
0
 
MarkAuthor Commented:
In my case, it is not an issue killing the conjobs as well, I simply selected the wrong answer.
0
 
omarfaridCommented:
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.
0
 
MarkAuthor Commented:
I thought it originally an issue but it is not
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now