Help with kill a specific process after certain running time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with kill a specific process after certain running time
# 1  
Old 03-10-2013
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.
Code:
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 program if it is running a specific task more than 1 minute times.
Thus the java program can continue to run other task instead of stuck at particular process which taken long time.

Thanks for any advice.
# 2  
Old 03-10-2013
Code:
# background process
java java.jar task_run.txt > /dev/null 2>&1   &
# take bg process id
PID=$!
# wait 60 s
sleep 60
# you can kill the process - if it's already stopped, then no problem
# send default signal TERM to the java process, it if's still working 
# = java can handle signal
kill $PID 2>/dev/null
sleep 2
# still process ? use signal KILL = and I mean it
kill -KILL $PID  2>/dev/null

# 3  
Old 03-10-2013
Hi,

Thanks for advice.
I will have a try on it.
I just google and it shown that ulimit -t able to do similar task too?
But I just not sure how to specific only automatic kill the java program.
I got run few thing at the same time.
But I just wanna kill if a java process run more than 30 seconds.

Thanks a lot.

---------- Post updated at 10:47 AM ---------- Previous update was at 10:26 AM ----------

I was thinking to write a bash script with able to print out all my java program pid all the time and it only kill the java running if it was running more than 10 seconds.
# 4  
Old 03-11-2013
Maybe something like following:
Code:
 
while sleep 10; do
  ps ax | grep "task_run.txt" | grep -v "grep" > line.txt
  if [ ! -s line.txt ]; then continue; fi
  minutes=`cat line.txt | cut -c 23 -d " "`
  pid=`cat line.txt | cut -f 1`
  if [ $minutes -gt 0 ]; then
    kill -9 $pid
    echo killed $pid
  fi
done

The "minutes" may be a little weak if the process continues to run but does not use cpu cycles. I think ps can be customized to display user time, if needed.
This User Gave Thanks to hanson44 For This Post:
# 5  
Old 03-11-2013
ionotifywait is not available in Redhat 5.6

Hi,

I need to monitor the file creation in some directory and i came to know that it can be achieved using ionotifywait tool but this is not available in Redhat 5.6

Do you have any idea wthether i can monitor the file without using inotifywait tool? Any other way to monitor the file creation file?

-Pawan


Moderator's Comments:
Mod Comment @Panwan Kumar: please start a new thread for this

Last edited by Scrutinizer; 03-11-2013 at 06:34 PM..
# 6  
Old 03-11-2013
Quote:
Originally Posted by Pawan Kumar
Hi,

I need to monitor the file creation in some directory and i came to know that it can be achieved using ionotifywait tool but this is not available in Redhat 5.6

Do you have any idea wthether i can monitor the file without using inotifywait tool? Any other way to monitor the file creation file?

-Pawan
It is rude and inconsiderate to hijack another person's thread. You should start your own. Otherwise, confusion may ensue regarding to which problem a response pertains, particularly if the response doesn't quote an earlier post.

I encourage others to not respond to hijacks.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to know all the Current running process and how to kill

All, 1.What is the unix comand used for all current running process (Including All current running processes Parent ->child->subchild process) 2.If child and subchild processes are running then what is the unix command to kill parent and its all child subchild processes in UNIX. Kindly... (7 Replies)
Discussion started by: skp
7 Replies

2. Shell Programming and Scripting

Script to kill a process running at a certain port

Hello, I have multiple scripts (vlc1, vlc2,...vlc5) and as I do not know how to run them as upstart processes, I entered my script links into rc.local file. Here is the sample one for process vlc1: $ nano /etc/rc.local added below line into rc.local /var/bin/./vlc1 & Port nr of vlc1... (7 Replies)
Discussion started by: baris35
7 Replies

3. Shell Programming and Scripting

Kill long running script, if it crosses the threshold time

Hi, I need a script to kill the process if it running for long time. Inputs for the scripts: 1.test.sh (will be running fron cron scheduler) 2.1 hr (ie threshold_time - if the test.sh is running for more than 1 hr test.sh has to kill) Thanks, Divya (1 Reply)
Discussion started by: Divya Nochiyil
1 Replies

4. 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

5. UNIX for Advanced & Expert Users

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? (9 Replies)
Discussion started by: cokedude
9 Replies

6. Shell Programming and Scripting

Script to kill the specific process

Hi I have the process to kill regulary, but the PSID is dymatic change and not sure how to kill the specific process ID Check the tradekast_rvd is running , if such process, kill the els process id ps -e f |grep tradekast_rvd ps -ef |grep els then I kill els process id ... (2 Replies)
Discussion started by: linux_user
2 Replies

7. UNIX for Dummies Questions & Answers

See if process is running and kill it

I need a simple line to run from apple remote desktop's Unix terminal on multiple different machines to see if Microsoft Entourage is running and kill it.. I imagine this could be done with a ps auxwww and grab the second field, (PID) put it into a variable and do a kill $variable using awk, but... (5 Replies)
Discussion started by: glev2005
5 Replies

8. 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

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. UNIX for Dummies Questions & Answers

how to kill process while keeping the submitted job running

:confused: I have a process which was schedule to run from 8am - 6pm daily. The scripts will search & run the commands that i was predefined in the database. Ususally, there were about 6-7 jobs for each submission and each job takes about 1-2 hrs and running one by one. And, I have a cron job... (3 Replies)
Discussion started by: hk_newbie
3 Replies
Login or Register to Ask a Question