Solved

csh script to kill processes

Posted on 2007-04-05
12
759 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX    Volume group Auto ON/OFF question 2 90
Linux : can't create transaction lock error 1 59
NTP Server in VMware 5 164
Wrap Oraccle SQL*Plus executable Command 4 69
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

895 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

14 Experts available now in Live!

Get 1:1 Help Now