4

我有这个脚本:

FINISH=0;
trap 'FINISH=1' SIGINT

INTERVAL=100;

while true
do
    START=`date +%s`;
    php-cgi -f process.php;
    STOP=`date +%s`;
    ELAPSED=$(($STOP-$START));
    SLEEP=$(($INTERVAL-$ELAPSED));
    if [ $SLEEP -gt 0 ]
    then
        echo "sleeping for $SLEEP seconds";
        sleep $SLEEP;
    fi
    if [ $FINISH -eq 1 ]
    then
        echo "exit";
        break;
    fi
done

但它不能像我想要的那样工作 - 我希望它只设置 FINISH=1 但它会杀死当前执行的命令(php-cgi 或睡眠) - 如何避免这种情况?其实我不希望它杀死 php-cgi ......

4

1 回答 1

2

这可能对你有用。

#!/bin/bash
trap 'exit' SIGINT

interval=100;

while true
do
    start=$(date +%s)
    nohup php-cgi -f process.php
    stop=$(date +%s)
    ((elapsed = stop - start))
    ((sleep = interval - elapsed))
    if (( sleep > 0 ))
    then
        echo "sleeping for $sleep seconds"
        sleep "$sleep"
    fi
done
于 2012-05-29T18:49:52.620 回答