Link to home
Start Free TrialLog in
Avatar of dkingsf
dkingsf

asked on

C-shell script to kill process(s)

Don't know much about c-shell programming.  I need a c-shell or bourne shell script that can be run to check for a specific process name, AND owner and issue a kill -9 for that PID.  I'll award 200 points just because it's hard for me, but not necessarily for you.

Can't be perl or C by the way......
ASKER CERTIFIED SOLUTION
Avatar of newmang
newmang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newmang
newmang

Sorry - I should have mentioned that the command to run this script is as follows:

./the_script userid taskname


Cheers - Gavin
#!/bin/csh -f
if ($#argc < 2) then
    echo "Usage: $0 <username> <processname>"
    exit 1
endif
set os = `uname -s`
switch ($os)
  case 'SunOS':
     set pids = (`ps -ef|awk '($1=="'$1'"&&$9=="'$2'"){print $2};'`)
     breaksw
  case Linux:
     # NOTE that we assume that Linux uses gawk
     set pids = (`ps axu | awk '($1=="'$1'"&&$11=="'$2'"){print $2};'`)
     breaksw
  default:
     echo "**ERROR: unknown OS: $os"
     breaksw
endsw
if ($#pids > 0) then
    kill -9 $pids
else
    echo no matching processes
endif
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Points split newmang & ahoffmann

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster
Cleanup Volunteer
Finalized as proposed

ahoffmann points at:
https://www.experts-exchange.com/questions/20569250/Points-for-ahoffmann.html

modulo

Community Support Moderator
Experts Exchange