Solved

Write a BASH script that kills a bunch of processes

Posted on 2011-09-20
7
373 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

After running Ubuntu some time, you will be asked to download updates for fixing bugs and security updates. All the packages you download replace the previous ones, except for the kernel, also called "linux-image". This is due to the fact that w…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

914 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

19 Experts available now in Live!

Get 1:1 Help Now