2

I have a shell script which looks like this:

echo firstOutput
read -s
command0
command1a
command1b
command1c
echo secondOutput
command2

command1a, command1b, command1c are individual commands but they can be executed in arbitrary order and are a group in the sense of the following explanation.

I now want to improve it so the script continues even if one command gets stuck. command0 needs to be executed first and it got 8 seconds to operate. If it doesn't make it within 8 seconds, the script shall continue to command1[a-c] which together have 5 seconds to operate. If they don't terminate within the given 5 seconds, command2 shall be called. Just in case this matters: command2 is the last command of this script.

Edit: To clarify: I know how to make a script which executes the commands in the maximum time allowed. However, these are timeouts and I want the script to run through as fast as possible. The entire script usually runs through in a fraction of a second after enter is pressed (see line read -s).

3
echo firstOutput
read -s

command0 &
sleep 8

command1a &
command1b &
command1c &
sleep 5

echo secondOutput
command2
  • Exactly what I would have done... Upvoted! ;-) – Fabby Apr 12 '15 at 22:38
0

The timeout command does exactly what you seem to want. See man timeout:

NAME
   timeout - run a command with a time limit

SYNOPSIS
   timeout [OPTION] DURATION COMMAND [ARG]...
   timeout [OPTION]

DESCRIPTION
   Start COMMAND, and kill it if still running after DURATION.
  • The problem being that timeout will kill the command, which doesn't look like what the OP intents. OP just wants the next set of commands to start once the timeout expires. – muru Apr 10 '15 at 3:27
  • @muru That's right. I want the previous commands to continue running when the other commands are called. I don't want any of them to be forcefully terminated. – UTF-8 Apr 10 '15 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.