Solved

Write a BASH script that kills a bunch of processes

Posted on 2011-09-20
7
374 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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