Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Write a BASH script that kills a bunch of processes

Posted on 2011-09-20
7
Medium Priority
?
379 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
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.

 
LVL 4

Expert Comment

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

Accepted Solution

by:
sakman earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you sitting there reading this and wondering how to get started with Linux? It almost seems like picking the right Linux distribution is about like picking the right college or buying a new car if you read some of the article out there. Relax… l…
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…
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 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…
Suggested Courses

670 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