Do something every few seconds/minutes/hours in a shell script
In this case "something" is a command that prints current date and time.
Every 5 seconds:
while true; do
if [[ $(date +%S) =~ (0$|5$) ]] ; then
echo Current time is ... $(date)
sleep 1
fi
done
Current time is ... Thu Jul 28 15:12:20 PDT 2011
Current time is ... Thu Jul 28 15:12:25 PDT 2011
Current time is ... Thu Jul 28 15:12:30 PDT 2011
Current time is ... Thu Jul 28 15:12:35 PDT 2011
Current time is ... Thu Jul 28 15:12:40 PDT 2011
Current time is ... Thu Jul 28 15:12:45 PDT 2011
Current time is ... Thu Jul 28 15:12:50 PDT 2011
Every 10 seconds:
while true; do
if [[ $(date +%S) =~ (0$) ]] ; then
echo Current time is ... $(date)
sleep 1
fi
done
Current time is ... Thu Jul 28 15:13:00 PDT 2011
Current time is ... Thu Jul 28 15:13:10 PDT 2011
Current time is ... Thu Jul 28 15:13:20 PDT 2011
Current time is ... Thu Jul 28 15:13:30 PDT 2011
Current time is ... Thu Jul 28 15:13:40 PDT 2011
Current time is ... Thu Jul 28 15:13:50 PDT 2011
Every 15 seconds:
while true; do
if [[ $(date +%S) =~ (00|15|30|45) ]] ; then
echo Current time is ... $(date)
sleep 1
fi
done
Current time is ... Thu Jul 28 15:14:15 PDT 2011
Current time is ... Thu Jul 28 15:14:30 PDT 2011
Current time is ... Thu Jul 28 15:14:45 PDT 2011
Current time is ... Thu Jul 28 15:15:00 PDT 2011
Every 20 minutes:
while true; do
if [[ $(date +%M) =~ (00|20|40) ]] ; then
echo Current time is ... $(date)
sleep 60
fi
done
Current time is ... Thu Jul 28 15:20:00 PDT 2011
Current time is ... Thu Jul 28 15:40:00 PDT 2011
Current time is ... Thu Jul 28 16:00:00 PDT 2011
Current time is ... Thu Jul 28 16:20:00 PDT 2011
0 comments:
Post a Comment