This is on a CentOS box, I have two scripts that need to run in order.
I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours (that I specify of course), and then calls the second script (they can't run simultaneously) which will self terminate once it's complete.
I'm unsure how to do the middle part, that is terminating the first script after a certain time limit, any suggestions?
You can start the first program in a subshell background process, record the process id and use the sleep
command to wait x number of hours. Then you can use the process id to kill the program, after which you can start the second one...
1 Like
Subshell? I think you mean a background process.
Killing a background process ID $! is tricky; e.g. the PID can have been re-assigned to another program in the meantime.
Job control is better:
script1 &
sleep 3600
kill %1 2>/dev/null
script2
In CentOS 6 there is /usr/bin/timeout, that will immediately run script2 when script1 has terminated.
timeout 3600 script1
script2
Yes I meant a background process, corrected in my post..