LINUX.ORG.RU

я когда автобилд систему писал, в таком духе делал:

############################################
boa_inilock somelk 0

(
    echo SOMETHING LAUNCHED.. 1>&2
    something
    echo SOMETHING EXITED 1>&2

    if boa_setlock_tofrom_noblock somelk 1 0; then
        analyze_results
    else
        echo FALLBACK ALREADY HERE, GIVING UP 1>&2
    fi
) &

if ! boa_setlock_tofrom_wait somelk 2 1 10000; then
    if boa_setlock_tofrom_noblock somelk 3 0; then
        echo SOMETHING HAS NOT COMPLETED ITS MISSION 1>&2
        echo KILLING IT.. 1>&2
        kill -9 $!
        echo KILL RETURNED $? 1>&2

        analyze
    else
        echo FALLBACK NOT NEEDED 1>&2
    fi
else
    echo FALLBACK NOT NEEDED 1>&2
fi

boa_setlock_tofrom somelk 4 3
############################################

Тут я использую свои функции для работы с локами:

# (locks are implemented using mv -- according to POSIX it should be atomic and race-free..)
# initializes lock $1 to state $2
boa_inilock()
{
    echo inilock $1 to $2 1>&2
    rm -f ${BOA_PATH}/${1}_lock_*
    touch ${BOA_PATH}/${1}_lock_${2}
}

# echoes to stdout current state of lock $1
boa_getlock()
{
    echo ${BOA_PATH}/${1}_lock_* | xargs -n1 basename | head -n1 | sed "s/^${1}_lock_//"
}

# tries to set lock $1 to state $2 from state $3
# returns 0 on success, 1 on failure
boa_setlock_tofrom_noblock()
{
    lock_to=${BOA_PATH}/${1}_lock_${2}
    lock_from=${BOA_PATH}/${1}_lock_${3}
    if test -e $lock_from && mv $lock_from $lock_to 2> /dev/null; then
        echo lock $1 is set to $2 from $3 1>&2
        return 0
    fi
    return 1
}

# tries to set lock $1 to state $2 from state $3
# returns only on success
boa_setlock_tofrom()
{
    echo trying to set lock $1 to $2 from $3, blocking mode 1>&2
    lock_to=${BOA_PATH}/${1}_lock_${2}
    lock_from=${BOA_PATH}/${1}_lock_${3}
    while ! (test -e $lock_from && mv $lock_from $lock_to); do sleep $BOA_LOCKSTEP; done 2> /dev/null
    echo ok -- set lock $1 to $2 from $3, blocking mode 1>&2
    return 0
}

# tries to set lock $1 to state $2 from state $3
# it waits lock to appear in state $3 for $4 seconds, if lock appears in state $3 then lock is set to state $2 and it returns 0
# if this state has not appeared in elapse of $4 seconds then it returns 1
boa_setlock_tofrom_wait()
{
    echo trying to set lock $1 to $2 from $3, waiting $4 seconds 1>&2
    if test $4 -eq 0; then
        return boa_setlock_tofrom_noblock $1 $2 $3
    fi
    WAIT=$(expr $4 / $BOA_LOCKSTEP + 1)
	while test $WAIT -gt 0; do
        sleep $BOA_LOCKSTEP
        if boa_setlock_tofrom_noblock $1 $2 $3; then
            return 0
        fi
		WAIT=$(expr $WAIT - 1)
    done
    echo trying to set lock $1 to $2 from $3 -- $4 seconds elapsed, giving up 1>&2
    return 1
}

dilmah ★★★★★
()
Ответ на: комментарий от anonymous

локи можешь убрать, если хочешь.

Ключевые слова:
( something ) &
sleep N
kill $!

dilmah ★★★★★
()

#!/bin/sh <путь-к-исполняемому=файлу> & 2>&1(параметры по желанию) RESULT=`ps ax | grep <путь-к-исполняемому=файлу>`(внимательнее на апострофы) sleep 4(тут указываете необходимое количество секунд) if [ -n "$RESULT" ]; then killall -9 <путь-к-исполняемому=файлу> fi

anonymous
()
Ответ на: комментарий от anonymous

очень уж ненадежно с этими ps и grep.

my_command &
sleep 60
kill $!

так тоже не очень хорошо, но проще и лучше.

idle ★★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.