Solved

Write a BASH script that kills a bunch of processes

Posted on 2011-09-20
7
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating a Samba server for a small office. Ubuntu Linux and Samba can breathe new life into a retired PC and save an office money on new hardware/software. Our example server will have two hard disks, one exclusively for storing shared data. …
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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