Solved

csh script to kill processes

Posted on 2007-04-05
12
767 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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 …
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

770 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