Shell Script to Kill Process(number of process) Unix/Solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to Kill Process(number of process) Unix/Solaris
# 1  
Old 11-23-2009
Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it

This is how we execute the script (and this is the requirement)
------------------------------------
bash-2.05$ processkill
Usage: ./processkill [-p|-f] [-u username] [num] regex
Kill processes matching a regular expression
-p: print processes matching regular-expression, don't kill
-f: force (don't ask)
num: send specified number to process. 15 if none given
bash-2.05$
bash-2.05$
bash-2.05$
bash-2.05$ processkill -u <username> 9 <process_name>
3937 /app/share/java/1.4.2_08/bin/java -Djava.library.path=/app/shar
5269 /app/share/java/1.4.2_11/bin/java -Xms128m -Xmx256m -Djava.library.path
55824 /app/share/java/1.4.2_11/bin/java -Xms128m -Xmx256m -Djava.library.path
349187 /app/share/java/1.4.2_08/bin/java -classpath /app/share/config:
Kill these processes? (y|n)n
No processes killed

If Yes, it kill all the above mentioned process
------------------------------------


But now we are getting this error part when we try to execute the script
-------------------------------------

./killproc: line 9: my: command not found
./killproc: line 10: my: command not found
./killproc: line 11: my: command not found
./killproc: line 12: my: command not found
./killproc: line 14: syntax error near unexpected token `{'
./killproc: line 14: `if($#ARGV==-1 || $ARGV[0]=~/-h/){'
------------------------------------


This is the Script.
-----------------------------------

#!/usr/bin/perl -w
#
# Kill processes matching a regular expression

# TODO:
# -Use perl function to access process managment instead of @ps=`ps ...`

my $force=0;
my $print_only=0;
my $user_name="";
my $regex;

if($#ARGV==-1 || $ARGV[0]=~/-h/){
print "Usage: $0 [-p|-f] [-u username] [num] regex\n" .
" Kill processes matching a regular expression\n" .
" -p: print processes matching regular-expression, don't kill\n" .
" -f: force (don't ask)\n" .
" num: send specified number to process. 15 if none given\n";
exit;
}

if($ARGV[0]=~/^-p/){
$print_only=1;
shift(@ARGV);
}
elsif($ARGV[0]=~/^-f/){
$force=1;
shift(@ARGV);
}

if($ARGV[0]=~/^-u/){
shift(@ARGV);
$user_name = shift(@ARGV);
# print"*** $user_name\n\n";
}

$signal=15;

if($ARGV[0]=~/^\d*$/){
$signal=$ARGV[0];
shift(@ARGV);
}

$regex=$ARGV[0];


#@ps=`ps --no-header -Ao pid,args`;
if ($user_name)
{
@ps=`ps -u $user_name -o pid,args`;
}
else
{
@ps=`ps -Ao pid,args`;
}

$kill_apps="kill -$signal ";
$num2kill=0;
shift @ps;
foreach $ps (@ps){
$ps =~ /^\s*(\d*)\s*(.*)$/;
$pid=$1; # process-ID
$command=$2;
if($command=~/$regex/ && $$!=$pid){
if($print_only or !$force){
print "$pid $command\n";
}
$kill_apps.="$pid ";
$num2kill++;
}
}

if(!$print_only && $num2kill!=0){
$answer="y";
if($force==0){
print "Kill these processes? (y|n)";
$answer=<STDIN>;
}
if($answer=~/^y/ or $answer=~/^Y/){
print $kill_apps;
print `$kill_apps 2>&1`;
print "\n";
}
else{
print "No processes killed\n";
}
}


-----------------------------------


Is there any basic thing we are missing? could any one help us

Regards
jonnyvic
# 2  
Old 11-23-2009
wrong path

hello buddy,
I parsed your code and practically everything seemed to be correct.
I feel you there is something wrong with the interpreter line.
check the path of your perl binary aka #which perl
so the first line should be
#!<the_actual_path_that_is_returned_by_which perl>
probably in your earlier env you would have been having this path
/usr/bin/perl
but perl is installed in some other dir.
correct the first (interpreter line ) as stated above and your code will run fine.
Hope you get it working.
ttyl
# 3  
Old 12-03-2009
Thank for the info buddy, the path is wrong.. i chnaged the path it started working fine...

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. UNIX for Beginners Questions & Answers

Solaris, grant user to kill another process

the task is grant user1 to kill another (for example user2) process. My steps: by root: usermod -P "Process Management" user1 login user1 user1@server (~) pfexec kill <PID> the result is: ksh: <PID>: not found or user1@server (~) pfexec pkill <PID> the result: nothing happens, still... (0 Replies)
Discussion started by: dsyberia
0 Replies

3. Shell Programming and Scripting

shell script to find a process by name and kill it

hi, Am a newbie to unix and wasnt able to write script to my requirement. I need a shell script, which should find a process by name and kill it. For eg: let the process name be "abc". I have different processes running by this name(abc), so should kill them all. Condition would be: if... (7 Replies)
Discussion started by: fop4658
7 Replies

4. Shell Programming and Scripting

Cron job and shell script to kill a process if memory gets to high

Hello, I'd like to set a cron job that runs a shell script every 30 minutes or so to restart a java based service if the memory gets above 80%. Any advice on how to do this? Thanks in advance! - Ryan (19 Replies)
Discussion started by: prometheon123
19 Replies

5. Shell Programming and Scripting

Script for to kill the process Ids when restarting the unix server

Hi, I need a script to kill the process Ids for the user ABC. I prepared the following script after that while logging with user therough script i am not sure how to pass the user name and password.Can ou modify the script and help me out. #!/bin/bash for filesize in $(ls -ltr | grep... (4 Replies)
Discussion started by: victory
4 Replies

6. Shell Programming and Scripting

shell script to kill process with respect to terminal

Hi, I've a script which kills all process, but i need a script shell script(sh), where it'll kill process on that particular terminal. below is example TY=`tty` for P in $TY do `kill -9 $P 2>/dev/null`; done echo "test process killed" break ... (3 Replies)
Discussion started by: asak
3 Replies

7. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

8. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

9. Shell Programming and Scripting

Unix Script to find and kill a process with high memory utilization

Hi Unix Gurus i am somewhat new to unix scripting so need your help to create a script as below. # This script would find the process consuming memory beyond a certain #limit. if the meemory consumption is more than 100% for a period of 1 # minute for the specific process. the script would... (0 Replies)
Discussion started by: robinforlinux
0 Replies

10. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies
Login or Register to Ask a Question