Solved

csh script to kill processes

Posted on 2007-04-05
12
794 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

632 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