Bash: Kill Process after timeout or after an event -


i have script runs long running process. process stopped after timeout.

#!/bin/bash timeout 3600 ./longrunningprocess 

my problem process not return before timeout reached , need stop earlier.

what need?

i want start other script in parallel checks periodically if longrunningprocess should stop. when bash script returns, timeout command should killed.

any idea how achieve that?

is there timeout command? not timespan script start , event trigger?

e.g.

#!/bin/bash fancycommandkillssecondcommandiffirstcommandreturns "./myperiodicscript.sh" "timeout 3600 ./longrunningprocess" 

thanks!

edit: "start 2 processes in parallel , kill both if 1 returns" work...

edit2: answers gave me ideas script:

#!/bin/bash firstprocess="${1}" secondprocess="${2}"  exec $firstprocess & pid1=$! exec $secondprocess & pid2=$!  function killall {     if ps -p $pid1 > /dev/null             kill -9 $pid1     fi     if ps -p $pid2 > /dev/null             kill -9 $pid2     fi } trap killall exit  while true;     if ! ps -p $pid1 > /dev/null            exit;     fi     if ! ps -p $pid2 > /dev/null            exit;     fi     sleep 5; done 

this kind of want. there native functionality or better way this?

start longrunningprocess in background , remember pid.

#!/bin/bash timeout 3600 ./longrunningprocess & long_pid=$! ./myperiodicscript.sh kill -9 ${long_pid} 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -