Solved

Bash script to kill all running PHP scripts?

Posted on 2014-12-22
17
258 Views
Last Modified: 2015-01-15
?
0
Comment
Question by:Mark
  • 6
  • 5
  • 4
17 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 40514376
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
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40514609
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 40514625
Thank you Gerwin for caching the typo
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 40

Expert Comment

by:omarfarid
ID: 40514627
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
 

Author Comment

by:Mark
ID: 40514928
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40514977
Can you try;

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

Author Comment

by:Mark
ID: 40529205
ps -ef | grep -v grep | grep -i "php ":

Outputs running processes containing PHP, need a list of processes that begin with "php "
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40529255
please give example
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40529563
>> 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
 

Author Comment

by:Mark
ID: 40539252
It works, thanks
0
 

Author Comment

by:Mark
ID: 40541637
i meant his answer:
kill -9 $(ps -ef | grep -v grep | grep -i php | awk '{ print $2 }')
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40541741
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
 

Author Comment

by:Mark
ID: 40541950
In my case, it is not an issue killing the conjobs as well, I simply selected the wrong answer.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40541992
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
 

Author Comment

by:Mark
ID: 40543453
I thought it originally an issue but it is not
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question