Solved

csh script to kill processes

Posted on 2007-04-05
12
752 Views
Last Modified: 2008-02-01
in csh, how would I write a script that would kill all processes whose names begin with "abc"?
0
Comment
Question by:adg
  • 8
  • 2
  • 2
12 Comments
 
LVL 3

Author Comment

by:adg
ID: 18865770
Wow, I though this would be easy! Am I asking in the right place?
0
 
LVL 4

Accepted Solution

by:
JulieBouillon earned 500 total points
ID: 18867699
kill `ps -ef | awk '{print $8" "$2}' | grep "^abc" | cut -f2 -d" "`

Use at your own risk ;-)


PS: check the ouput of the "ps -ef" command on your unix and make sure that the PID is the 2nd column and the process the 8th column.
0
 
LVL 3

Author Comment

by:adg
ID: 18867918
Thanks for the response! Its Solaris, BTW. I just checked - PID is the 2nd column and CMD is the 8th. This is on my own workstation so if I kill all the processes by mistake I'm guessing I'd just have to reboot (I hope).

I'll try it now and get back to you soon.
0
 
LVL 3

Author Comment

by:adg
ID: 18868200
OK, I tried it and we're very close. If I run the part without the kill (just the ps, awk, grep and cut) I get a list of the correct PIDs. But when I add the kill and the single quotes I get the message kill: invalid id.  It happens no matter how many matching pid's there are.  4, 1 0 - no difference.

There is something wrong with the way I'm passing the list of PID's to the kill.  I'm going to read the man to see if I can figure out why but I just wanted to let you know in case you have any ideas.

Thanks!

0
 
LVL 3

Author Comment

by:adg
ID: 18868361
I think the problem is that there is still newlines in the list of pid's. IOW each pid is on a separate line.  Now I'm looking for a way to remove the newlines. I tried adding another delimited to the cut, i.e, -d"\n" but that caused an error.
0
 
LVL 3

Author Comment

by:adg
ID: 18868499
OK, I used  tr "\n" " " to solve my newline problem.  The expression starting from the ps works perfectly. If I send it to a file and then manually edit it to put kill in front and then run it, it works. But it doesn't work as written above where the entire expression is inside single quotes and passed to kill as a parameter. How can I
0
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

 
LVL 4

Expert Comment

by:JulieBouillon
ID: 18868570
you might replace the cut with: awk '{printf $2" "}'

you will then have:
kill `ps -ef | awk '{print $8" "$2}' | grep "^abc" | awk '{printf $2" "}'`

0
 
LVL 3

Author Comment

by:adg
ID: 18868593
Thanks I got it now. The last problem I was having is that I thought it was single quotes surrounding the commands. Now I undersatnd that it was back quote (grave).
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18879044
An easier, more reliable way is:

kill `ps -ef|awk '$NF~/^abc/ {print $2}'`

or if your Unix flavour has the pkill command, simply do

pkill abc
0
 
LVL 3

Author Comment

by:adg
ID: 18887422
>> An easier, more reliable way is:

What makes it more reliable?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18887529
pkill is the best method as it is designed for this type of use.

My solution is more reliable, as it doesn't matter if your version of ps has the command in column 8, or some other column.
0
 
LVL 3

Author Comment

by:adg
ID: 18887592
Oh, I see - thanks! I'll give it a try.
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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

760 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

25 Experts available now in Live!

Get 1:1 Help Now