Killing a process that takes too long


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing a process that takes too long
# 1  
Old 04-05-2006
Killing a process that takes too long

Hello,
I have a C program that takes anywhere from 5 to 100 arguments and I'd like to run it from a script that makes sure it doesnt take too long to execute. If the C program takes more than 5 seconds to execute, i would like the shell script to kill it and return a short message to the user. If it takes less than 5 seconds, the program spits out a short message to standard out, then i'd like the script to stop normally.

Can a script help do this?, or am I going to have to rewrite the C program to handle this?

The problem is, it seems like i would need a multi-threaded shell script?...I'm really not sure how to go about doing this. Any help would be greatly appreciated. I'm running bash 3.00.15 on redhat.

Thanks
# 2  
Old 04-05-2006
Try this if you don't want to change your c program

#!/usr/bin/perl -w

my $timeout = 5; # 5 sec timeout

eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeout;
qx(./mah.ksh);
alarm 0;
};

if ($@) {
die unless $@ eq "alarm\n";
print "timeout\n";
qx(ps -ef | grep mah.ksh | egrep -v " grep | vi | more | cat | pg | egrep " | awk '{print \$2}' | xargs kill );
}
else {
print "no timeout \n";
}

replace mah.ksh with your c program name.
# 3  
Old 04-06-2006
Thanks...that script works great Smilie

Although I'm still having problems...I modified your perl script to be able to take command line arguments...is there a faster way than the way I did it?

The main problem however is that whenever I have php4's shell_exec command calling this script, nothing that is printed via perl's qx command gets returned to php4...i tried 'system($command)' rather than 'qx($command)', but that doesn't help...interestingly though, 'print system($command)' returns a -1 to php, but from a terminal, all of those run the program as desired...

how can i get shell_exec to see output from a call to qx?...is qx opening a new stream each time it is called or something?

here's my perl modified script:

Code:
#!/usr/bin/perl -w

# Run my_program w/ the given arguments.  If my_program is still running 
# after the specified number of seconds, kill it, and tell the user.

my $timeout = 5; # 5 second timeout

eval {
  local $SIG{ALRM} = sub { die "alarm\n" };
  alarm $timeout;
  $command = "./my_program";
  foreach $argnum (0 .. $#ARGV) {
    $command .= " $ARGV[$argnum]";
  }
  print qx($command);
  alarm 0;
};

if($@){
  die unless $@ eq "alarm\n";
  print "timeout\n";
  qx(ps -ef | grep pokenum | egrep -v " grep | vi | more | cat | pg | egrep " | awk '{print \$2}' | xargs kill );
}

# 4  
Old 04-10-2006
just assign it to some variable.... and then print

my $xyz = qx(unix command);
print $xyz,"\n";
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script takes too long to complete

Hi, I have a lengthy script which i have trimmed down for a test case as below. more run.sh #!/bin/bash paths="allpath.txt" while IFS= read -r loc do echo "Working on $loc" startdir=$loc find "$startdir" -type f \( ! -name "*.log*" ! -name "*.class*" \) -print | while read file do... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

3. Shell Programming and Scripting

Find command takes long

Hi, I am trying to search for a Directory called "mont" under a directory path "/opt/app/var/dumps" Although "mont" is in the very parent directory called "dumps" i.e "/opt/app/var/dumps/mont" and it can never be inside any Sub-Directory of "dumps"; my below find command which also checks... (5 Replies)
Discussion started by: mohtashims
5 Replies

4. UNIX for Advanced & Expert Users

Find command takes too long to complete

Hi, Below is my find command find /opt/app/websphere -name myfolder -perm -600 | wc -l At time it even takes 20 mins to complete. my OS is : SunOS mypc 5.10 Generic_150400-09 sun4v sparc SUNW,T5440 (10 Replies)
Discussion started by: mohtashims
10 Replies

5. UNIX and Linux Applications

database takes long time to process

Hi, we currently having a issue where when we send jobs to the server for the application lawson, it is taking a very long time to complete. here are the last few lines of the database log. 2012-09-18-10.35.55.707279-240 E244403536A576 LEVEL: Warning PID : 950492 ... (1 Reply)
Discussion started by: techy1
1 Replies

6. Shell Programming and Scripting

sort takes a long time

Dear experts I have a 200MG text file in this format: text \tab number I try to sort using options -fd and it takes very long! is that normal or I can speed it up in some ways? I dont want to split the file since this one is already splitted. I use this command: sort -fd file >... (12 Replies)
Discussion started by: voolek
12 Replies

7. UNIX for Dummies Questions & Answers

Changing Password process takes a long time

We are running unix. After a reboot of the server we have found that changing password takes a long time. if type in passwd "username" you can type in the 1st instance of the password , press enter , then it will wait for about 3 minutes before bringing up the confirm password line typing it in... (4 Replies)
Discussion started by: AIXlewis
4 Replies

8. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

9. Linux

it takes long time to login on server

Hi, I am trying to login using ssh on Red Hat Linux 5 server, The password appears immediately but after I enter the password it takes about 90 seconds to login completely. Please suggest what changes require? Regards, Manoj (4 Replies)
Discussion started by: manoj.solaris
4 Replies

10. UNIX for Advanced & Expert Users

Interrupt signal Control C takes too long to terminate a process

I have a process to terminate, and when keying Control C/ kill -int , it takes 15 minutes to half an hour to terminate the process. I've tried using kill -2, or keying control c twice, however the process seem to be killed abruptly, without writing into the log file. So the only way in order to... (8 Replies)
Discussion started by: paqui
8 Replies
Login or Register to Ask a Question