Skip to main content
added 6 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

With ksh/bash/zsh:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if read -t 3 status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

We duplicate the original stdout onto fd 3 (3>&1) so we can restore it for slowprocess.sh (>&3), while stdout for the rest of the (...) subshell goes to the pipe to read -t 3.

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:'sh;exit' 

would avoid slowprocess.sh to bebeing killed (the ;:;exit is necessary for sh implementations that optimise by executing the last command in the shell process).

With ksh/bash/zsh:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if read -t 3 status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

We duplicate the original stdout onto fd 3 (3>&1) so we can restore it for slowprocess.sh (>&3), while stdout for the rest of the (...) subshell goes to the pipe to read -t 3.

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

With ksh/bash/zsh:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if read -t 3 status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

We duplicate the original stdout onto fd 3 (3>&1) so we can restore it for slowprocess.sh (>&3), while stdout for the rest of the (...) subshell goes to the pipe to read -t 3.

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;exit' 

would avoid slowprocess.sh being killed (the ;exit is necessary for sh implementations that optimise by executing the last command in the shell process).

added 194 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

With bashksh/bash/zsh:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if TMOUT=3 read -t 3 status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

We duplicate the original stdout onto fd 3 (3>&1) so we can restore it for slowprocess.sh (>&3), while stdout for the rest of the (...) subshell goes to the pipe to read -t 3.

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

With bash:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if TMOUT=3 read status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

With ksh/bash/zsh:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if read -t 3 status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

We duplicate the original stdout onto fd 3 (3>&1) so we can restore it for slowprocess.sh (>&3), while stdout for the rest of the (...) subshell goes to the pipe to read -t 3.

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

added 19 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

With bash:

{ (./slowprocess.sh >&3 3>&-; echo ."$?") | if TMOUT=3 read completion;status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

With bash:

{ (./slowprocess.sh >&3 3>&-; echo .) | if TMOUT=3 read completion; then echo "Cool it completed, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

With bash:

{ (./slowprocess.sh >&3 3>&-; echo "$?") | if TMOUT=3 read status; then echo "Cool it completed with status $status, do stuff..." else echo "It didn't complete, do something else..." fi } 3>&1 

Alternatively, if you want to use timeout (here assuming GNU timeout):

timeout --foreground 3 sh -c './slowprocess.sh;:' 

would avoid slowprocess.sh to be killed (the ;: is necessary for sh implementations that optimise by executing the last command in the shell process).

added 169 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading