Solved

Write a BASH script that kills a bunch of processes

Posted on 2011-09-20
7
371 Views
Last Modified: 2012-05-12
I have a script that launches a bun of PHP scripts that run as Daemons (yes they really do). However the only way I can kill them is to do a px as | grep script.php and then kill the process ID.

The scripts when they launch store the PID in a subfolder of the same name in a file of the same name with a .pid extension.

I am trying to figure out how to write a BASH script by scanning all the subfolders of the current directory, getting the PID from a file and then killing the process. I have tried something like this but it didn't work as expected:

cat */*pid | xarg kill -9

Please help
0
Comment
Question by:skione
  • 5
  • 2
7 Comments
 
LVL 4

Expert Comment

by:sakman
ID: 36568742
skione,

Don't forget to quote the '*' so the shell doesn't interpret it.

For example, to find and list all the process PIDs in /var/run:

find /var/run -name '*.pid' | xargs cat {} \;

You could do something like:

for pid in `find . -name '*.pid' | xargs cat {} \;`
do
  kill -i $pid
done

Open in new window

0
 
LVL 4

Expert Comment

by:sakman
ID: 36568753
skione,

My typo:
kill -i $pid

Should be:
kill -9 $pid

0
 

Author Comment

by:skione
ID: 36568831
That looks like it should work but I am getting this:


./stop_serivces.sh
cat: {}: No such file or directory
cat: ;: No such file or directory
./stop_serivces.sh: line 4: kill: 100531000799541006510035: arguments must be process or job IDs


Mind you I am on Gentoo which sometimes has some odd behaviours
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:sakman
ID: 36568848
What does stop_services.sh look like?
0
 
LVL 4

Accepted Solution

by:
sakman earned 500 total points
ID: 36568863
You could skip the "xargs" line and run a loop like this:

for file in `find /var/run/ -name '*.pid'`
do 
  pid=`cat $file`
  echo "Process: $file ($pid)"
done

Open in new window

0
 

Author Comment

by:skione
ID: 36569126
Part of the issue was I had inadvertantly had a space at the end of the variable the specified the file name of the pid so some of the files where not 'file.pd' but 'file.pid '

I fixed this and used the second option. I also add a kill-9 $pid to the loop.

Thanks it works like a charm
0
 
LVL 4

Expert Comment

by:sakman
ID: 36569447
Cool.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now