Using the `before` and` / usr / bin / timeout` in the script

I want to execute a command that takes about 1 minute to complete in a bash script. However, sometimes this command freezes, so I want to use / usr / bin / timeout inside the loop until it works.

If I use timeout 300 mycommand myarg1, it works, but if I use it inside bash in this loop below, it doesn't print anything (not even the typical output that my command prints), and it freezes !:

until timeout 300 mycommand myarg
do
    echo "The command timed out, trying again..."
done

My bash version:

$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

My timeout version:

$ timeout --version
timeout (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

(Standard Ubuntu16.04)

+6
source share
5 answers

A problem with

(mycommand args) & pid=$!
sleep 1000 && kill -INT $pid

, 1000 . ( ) :

typeset -i i 
i=0
command with arguments &
pid=$!
while ps $pid > /dev/null 2>/dev/null ; do
    i=$i+1
    if [ $i -gt 999 ] ; then
        kill -9 $pid
    fi
    sleep 1
done

, :

command with arguments &
pid=$! 
echo "kill -9 $pid" | at "now + 15 minutes"
wait

timeout , .

,

until timeout 300 mycommand myarg
do
    echo "The command timed out, trying again..."
done

, bash until, 300 . timeout 300 mycommand myarg , , . , :

if timeout 30 mycommand myarg ; then
    echo "The timeout of mycommand succeeded"
else
    echo "It failed! What a pitty"
fi 
+2

.

:

sh timeoutLoop.sh 4

:

Seconds start now...
3 seconds passed...

:

sh timeoutLoop.sh 2

:

Seconds start now...
The command timed out, trying again...
Seconds start now...
The command timed out, trying again...
Seconds start now...
The command timed out, trying again...
Seconds start now...
The command timed out, trying again...
...

mycommand = waitSeconds.sh

#!/bin/bash -

echo "Seconds start now..."
sleep $1
echo "$1 seconds passed..."

bash timeoutLoop.sh

#!/bin/bash -

until timeout $1 waitSeconds.sh 3
do
echo 'The command timed out, trying again...'
done

, script.

+2

script - . , , . , , . , , . .

timeout=300
wait() {
    sleep 1 # sleep for one second
    echo -n . # show that we are still waiting
    [ `ps|grep $pid|wc -l` -gt 0 ] && { # background process still running?
        [ `ps -p $pid -o etimes|grep [0-9]` -gt $timeout ] && { # running too long?
            echo "Time expired.";
            kill $pid;
        } || {
            wait; # keep waiting
        }
    } || {
        echo "DONE!";
    }
}
sleep 20 & # replace this with mycommand &
pid=$!
wait
+1

, , , , - kill, 0 (), $? .

", , ..." - , .

, .

until timeout 20 find / -name "*.sh" 2>/dev/null ;do
  echo "The command timed out, not trying again..."
  break
done
+1

, . , :

(mycommand args) & pid=$!
sleep 1000 && kill -INT $pid

. . .

DVB-S:

(w_scan -a 7 -E 0 -f s -c $1 -s $2 -o 7 -x >$tunefile) & pid=$!
sleep 1500 && kill -INT $pid

w_scan .

EDIT: , pid: , , script .

if ps -p $id > /dev/null
then 
    : active
else
    : inactive
fi

EDIT2: script, ffmpeg . mkv ( 2Gb) mp4. , , 10 , .

, .

film=/home/wlgfx/longfilm.mkv
output=/home/wlgfx/longfilm.mp4

(ffmpeg -i $film -y -vcodec copy -acodec copy $output) & pid=$!
#for i in 'seq 1 10' # 10 seconds
#do
#    sleep 1
    if wait $pid; then echo "$pid success $?"
    else echo "$pid fail $?"
    fi
#done

$?

, ffmpeg 5 , 2Gb . , , script, , .

: fooobar.com/questions/67288/...

EDIT4: -, , , . .

film=/home/wlgfx/longfilm.mkv
output=/home/wlgfx/longfilm.mp4

while : ; do

    #(ffmpeg -i $film -y -vcodec mpeg2video -acodec copy $output) & pid=$!
    (ffmpeg -i $film -y -vcodec copy -acodec copy $output) & pid=$!

    (sleep 25 ; echo 'timeout'; kill $pid) & killpid=$!

    wait $pid # will get status if completed successfully
    status=$?

    (kill -0 $killpid && kill $killpid) || true

    [[ $status == 0 ]] || break

done
0

All Articles