Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-20-2008, 06:54 PM   #1
LQ Newbie
Registered: Oct 2008
Posts: 5

Rep: Reputation: 0
using pkill to kill a specific program after a certain time - help

Hi all,

I have a script which uses a program to convert a series of files. One of the features of the program is a continue function so it can span across disjointed files which are synchronous and it works on a 10second timer.

i.e. if it can find a new joined file in 10seconds then the program continues.

So the problem is that it is hard to batch this program up to do lots of files with this continue flag on as when it cant find the next joined file it just hangs forever and needs to be CTRL-C to kill.

So - finally the question.

How can i use pkill to specifically kill this program after a certain time because i know if i set the time to say 20secs then this is when it has finished and needs to be pkilled so the bash sh can continue...?

something like:

someprogram -i someinputfile
pkill someprogram if timer > 20sec

how do i write this in a sh script where i want to run the script but still keep control over a timer so it can kill it if it has hung?


Old 11-20-2008, 07:01 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You almost had it :}

someprogram -i someinputfile&
sleep 20
pkill someprogram
Old 11-20-2008, 11:20 PM   #3
LQ Newbie
Registered: Oct 2008
Posts: 5

Original Poster
Rep: Reputation: 0

will this work if somprogram doesnt terminate?

i thought it may need some parallel logic or the pkill with a timer before the command for someprogram..? Or the wait command then || pkill

I did some googling and came up with:

for t in $timeout $delay
while test $t -gt $interval
sleep $interval
kill -0 $$ || exit
t=`expr $t - $interval`
sleep $t
kill $SIG $$ && kill -0 $$ || exit
) 2> /dev/null &

exec "$@"

this looks sort of what i was thinking but i am a linux newbie so it is a little hard to understand what is going on.

How would you write this in a basic bash shell with the reference to just one program?

Old 12-07-2008, 02:55 PM   #4
LQ Newbie
Registered: Apr 2007
Posts: 3

Rep: Reputation: 0
Try this:

echo "pkill -KILL -f 'someprogram'" | at now + 20 minutes
someprogram -i someinputfile
echo "after the death of someprogram"



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kill specific Wine program from Command Line Brian114 Linux - Software 4 06-07-2011 04:02 AM
how to kill the process after specific time? samengr Linux - General 5 02-17-2010 11:19 AM
when a process cann't be terminated through kill/skill/pkill/killall... Osirix Linux - Software 9 07-18-2006 11:16 PM
run program automatically at specific time simon_qwl Programming 2 03-20-2005 10:58 AM
Running a program for a specific period of time CaptainHowdy Linux - Software 2 08-22-2004 07:02 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration