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

x
?
Solved

csh script to kill processes

Posted on 2007-04-05
12
Medium Priority
?
803 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
[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
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.

721 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