Trap signal and cleanup in while loop
bash
Solution
This might work for you.
#!/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
Problem
I have this script: ``` 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 ``` But it doesnt work as I would like - I want it to just set FINISH=1 but it kills currently executed command (php-cgi or sleep) - how to avoid this? Actually I don't want it to kill php-cgi...