Kill a process after a certain amount of time


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Kill a process after a certain amount of time
# 1  
Old 05-11-2012
Kill a process after a certain amount of time

I would like to kill a process after a certain amount of time. Can I please get some ideas on how to do this?

Last edited by cokedude; 05-11-2012 at 05:19 PM..
# 2  
Old 05-11-2012
Do you mean after a certain amount of time? You could record the pid of the process and start another background job that monitors the process with that pid and that will kill it when it runs too long..
# 3  
Old 05-11-2012
Quote:
Originally Posted by Scrutinizer
Do you mean after a certain amount of time? You could record the pid of the process and start another background job that monitors the process with that pid and that will kill it when it runs too long..
Yes. Sorry for not making that clear. I edited my question. Can you fix the topic title also? I'm pretty sure the red means your a mod Smilie.

Can you please give me a few ideas of how to do that?
# 4  
Old 05-12-2012
shell:
Code:
#!/bin/bash
# this creates a child process that commits patricide after n seconds
n=30
sleep $n && kill  $PPID  &
# go merrily on your way
#   and  do stuff until you get killed by your kid.  Sounds like a soap opera.

# 5  
Old 05-12-2012
You also should investigate the TMOUT environment variable
# 6  
Old 05-12-2012
Quote:
Originally Posted by jim mcnamara
shell:
Code:
#!/bin/bash
# this creates a child process that commits patricide after n seconds
n=30
sleep $n && kill  $PPID  &
# go merrily on your way
#   and  do stuff until you get killed by your kid.  Sounds like a soap opera.

So if wanted to start firefox and then kill it shortly would do something like this?

Code:
#!/bin/bash
# this creates a child process that commits patricide after n seconds
n=30
firefox
sleep $n && kill  $PPID  &
# go merrily on your way
#   and  do stuff until you get killed by your kid.  Sounds like a soap opera.

Or would I name the script something like kill and do this?

Code:
firefox kill
kill firefox

# 7  
Old 05-12-2012
Ok. You would have done better to have asked a complete question at the start. The example I gave you assumed that you started something like a shell script and wanted to time out the parent shell script. firefox is not a shell script, it is a standalone executable that runs in a child process so this method won't fly.

First off, kill [pid] is the way to stop a process. There may be variants of this like pkill for your OS - so what OS and shell are we talking about here?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with kill a specific process after certain running time

Hi, Do anybody experience to write a bash script in order to kill a specific process (java) after certain time of running? eg. java java.jar task_run.txt I will run a java program (java.jar) which will run a long list of process (task_run.txt) one by one. I plan to terminate the java... (5 Replies)
Discussion started by: perl_beginner
5 Replies

2. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

3. Shell Programming and Scripting

Determine amount of time to process

Hello all, Hopefully someone can point me in the right direction... I have a script written in bash which is pretty basic and just stop/starts various services based on particular conditions. What I am trying to build is a reporting type function which will send out an email with various stats... (3 Replies)
Discussion started by: systrex
3 Replies

4. Programming

kill a process which run out of time

hello everybody!! i want ur help! it is urgent!! ... pid=fork(); if(pid==0) { execl(a program); exit(1);} else if (pid>0) { timer(5); //(command 1)timer is a function that count up to 5sec if(kill(pid,0)==0)kill(pid,9);//(command 2) wait(&status); .... } else perror("error");... (3 Replies)
Discussion started by: nicos
3 Replies

5. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

6. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

7. Programming

kill(0,-9) don't kill the process

Hi all i have simple c program , when i wish to kill the app im using kill(0,-9) , but it seams this command don't do any thing and the program. just ignore it . what im doing wrong here ? im using HP-UX ia64 Thanks (9 Replies)
Discussion started by: umen
9 Replies

8. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies

9. UNIX for Dummies Questions & Answers

How ti kill a process which is consuming so much time

There is a process which is consuming too much time.. how to find that process and kill it. (3 Replies)
Discussion started by: shreenivas
3 Replies

10. Shell Programming and Scripting

executing a script for a certain amount of time

I am writing a script that takes two parameters: the name of another script and an integer that represents a number of seconds. The script must execute the second script (first parameter) for the specified number of seconds (second parameter), suspend it for the same number of seconds, and continue... (9 Replies)
Discussion started by: ponchorage
9 Replies
Login or Register to Ask a Question