timeout without killing process in bash

dougBTV

I have a main script that I'm running, and from it I have a second "slow process" I want to kick off, and "do something" in the main script if it doesn't complete in the time limit -- depending on if it completed or not. N.B. If the "slow process" finishes before my time limit, I don't want to have to wait an entire time limit.

I want the "slow process" to keep going so I can gather stats and forensics about it's performance.

I've looked into using timeout, however it will kill my script when finished.

Suppose this simplified example.

main.sh

result=`timeout 3 ./slowprocess.sh`
if [ "$result" = "Complete" ]
then
 echo "Cool it completed, do stuff..."
else
 echo "It didn't complete, do something else..."
fi

slowprocess.sh

#!/bin/bash
start=`date +%s`
sleep 5
end=`date +%s`
total=`expr $end - $start`
echo $total >> /tmp/performance.log
echo "Complete"

Here, it uses timeout -- so the script dies, so nothing winds up in /tmp/performance.log -- I want slowprocess.sh to complete, but, I want main.sh to go onto its next step even if it doesn't finish in the 3 seconds.

Stéphane Chazelas

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).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

run command in bash and then exit without killing the command

分類Dev

Killing process of a user before deleting

分類Dev

killing the master process spawned by an at command

分類Dev

Preventing the accidental killing of wrong process in htop

分類Dev

Bash script: ensure process has terminated without waiting unnecessarily

分類Dev

Killing shell=True process results in ResourceWarning: subprocess is still running

分類Dev

port's owner switched in netstat after killing the process

分類Dev

How to disable "Program '/bin/bash' crashed" warning when killing the shell?

分類Dev

bash read: group read results by timeout

分類Dev

Batch: Taskkill timeout without waiting for timer

分類Dev

Bash terminal stuck in process ([Process Completed])

分類Dev

How can I launch a command, wait 2 seconds and return the output without killing the command?

分類Dev

Does a bash subshell spawn a new `bash` process?

分類Dev

Conditionally run process in background with bash

分類Dev

Bash - wait for a process (here gcc)

分類Dev

`tee` and `bash` process substitution order

分類Dev

Linux - Isolate process without containers

分類Dev

Create persistent process without inittab

分類Dev

pkill not killing

分類Dev

How to timeout a Bash command and count the number of lines emitted to stdout?

分類Dev

bashで `tail`と` timeout`を使用する方法

分類Dev

In bash, is it generally better to use process substitution or pipelines

分類Dev

Bash: receive messages from background process

分類Dev

Controlling the environ passed to child process by bash

分類Dev

Bash: receive messages from background process

分類Dev

Attach, Detach and Kill a process in Screen with a bash script

分類Dev

Process a lot of data without waiting for a chunk to finish

分類Dev

call exe using Process without absolute path

分類Dev

Java GUI without any other process

Related 関連記事

  1. 1

    run command in bash and then exit without killing the command

  2. 2

    Killing process of a user before deleting

  3. 3

    killing the master process spawned by an at command

  4. 4

    Preventing the accidental killing of wrong process in htop

  5. 5

    Bash script: ensure process has terminated without waiting unnecessarily

  6. 6

    Killing shell=True process results in ResourceWarning: subprocess is still running

  7. 7

    port's owner switched in netstat after killing the process

  8. 8

    How to disable "Program '/bin/bash' crashed" warning when killing the shell?

  9. 9

    bash read: group read results by timeout

  10. 10

    Batch: Taskkill timeout without waiting for timer

  11. 11

    Bash terminal stuck in process ([Process Completed])

  12. 12

    How can I launch a command, wait 2 seconds and return the output without killing the command?

  13. 13

    Does a bash subshell spawn a new `bash` process?

  14. 14

    Conditionally run process in background with bash

  15. 15

    Bash - wait for a process (here gcc)

  16. 16

    `tee` and `bash` process substitution order

  17. 17

    Linux - Isolate process without containers

  18. 18

    Create persistent process without inittab

  19. 19

    pkill not killing

  20. 20

    How to timeout a Bash command and count the number of lines emitted to stdout?

  21. 21

    bashで `tail`と` timeout`を使用する方法

  22. 22

    In bash, is it generally better to use process substitution or pipelines

  23. 23

    Bash: receive messages from background process

  24. 24

    Controlling the environ passed to child process by bash

  25. 25

    Bash: receive messages from background process

  26. 26

    Attach, Detach and Kill a process in Screen with a bash script

  27. 27

    Process a lot of data without waiting for a chunk to finish

  28. 28

    call exe using Process without absolute path

  29. 29

    Java GUI without any other process

ホットタグ

アーカイブ