Programming and Scripting :: scheduling a task



Quote (_pathos @ Mar. 20 2006,01:30)
could this be done with a bash script?

Sure, try using sleep to put in a five minute pause:

Code Sample
#!/bin/bash
while [ 1 ]
do
   sleep 5m
   date ## replace this with your command(s)
done

Thank you all for your responses.

newolduser - I'm not very good at scripts yet but I may try to come up with something. I've done some google searches and haven't found anything except a lot of stuff about webpages and php.  

awphuch - i guess you mean after i install cron the crontab will be in /home/dsl/etc because it isn't there now.

clacker - what would you compare to? A list dates and tasks in a text file? Having this as a bash script would be very nice.

thanks again..

OK... at the risk of exposing myself as a lame coder.... I've taken a shot at a bash script to start the ball rolling.

This script reads a file called cronIN.txt  The file contains a list of commands you want executed. Each command is prefixed by a timestamp in the format YYYYmmddHHMM.  The last of the file should contain "<end>".

As commands are executed they are removed from cronIN.txt and put in cronLOG.txt with a timestamp of when they ran.

So a cronIN.txt file might look like this:
Code Sample

200603240300 echo Yikes
200603240230 doBackupStuff.sh
200612312359 echo happy new year
<end>



Here's the script. It's set to run every 5 minutes.  

THIS IS A VERY MUCH 'RUN AT YOUR OWN RISK' SITUATION

I'm sure when you look at it you'll find ways of making it smaller. The end-of-file thing needs work and I'm sure some checking could be done up front to make sure the cronIN.txt file exists.  The NICE command may have limits that I'm not aware of that would cause commands not to run. Like I said, it's a first shot. All improvements are welcome.

Code Sample

#!/bin/bash
version="0.01"
# fauxCron.sh for those without a cron...
#
# Required files:
#
#  1. cronIN.txt
#
#      Last line contains "<end>" in cols 1-5
#
#      All other lines should have timestamp in col 1-12
#                                                 blank in col 13
#                                                 command to be executed starting in col 14
#
#
#   2. cronLOG.txt
#
#      As commands are executed they are copied to this file with the
#      timestamp when they were executed put in front.
#
#
# Temporary file:
#
#   1. cronOUT.txt - temporary file that becomes cronIN.txt
#
while true
do
 exec 3< cronIN.txt
 thisRun=`date +%Y%m%d%H%M`
 echo $thisRun "fauxCron checking..."
 read taskTime lastRun <&3
 while [ $taskTime != "<end>" ]
 do
   read taskTime taskCommand <&3
   if [ $taskTime != "<end>" ]; then
     if [$taskTime -le $thisRun ]; then
       nice $taskCommand &
       echo $thisRun $taskTime $taskCommand >> cronLOG.txt
     else
       echo $taskTime $taskCommand >> cronOUT.txt
     fi
   fi
 done
 echo "<end>" >> cronOUT.txt
 exec 3>&-
 rm -f cronIN.txt
 cat cronOUT.txt > cronIN.txt
 rm -f cronOUT.txt
 sleep 5m
done
exit 0



Good luck :)

NewOld USer, I liked that example.  It's more involved and gives more options than what I was thinking.  I thought more of tasks that need to be run once or twice a day.  I was more concerned with that than the task running exactly at 5:00 for example.

Here was my code to check every 5 minutes if the database has changed and if so, let the user know:

Code Sample
#!/bin/bash
# an example of a timed action

OLD_MD5=`md5sum myfile`

while true
do
    sleep 5m  # sleep for 5 minutes
               # could be 5s or 12h,
               # whatever you need
    if ! [ "$OLD_MD5" = "`md5sum myfile`" ]
    then
         # if the md5sum on the file has changed, back it up
         OLD_MD5=`md5sum myfile` # save the new sum
         echo $OLD_MD5  # display the fact we are changing
         # do backup stuff here
    fi
done

Well.... in the light of day I see a problem with my version 0.01... It looks like it will miss the first entry in the cronIN.txt file.  I've moved the reads around a little and came up with version 0.02

Code Sample

#!/bin/bash
version="0.02"
# fauxCron.sh for those without a cron...
#
# Required files:
#
#  1. cronIN.txt
#
#      Last line contains "<end>" in cols 1-5
#
#      All other lines should have timestamp in col 1-12
#                                                 blank in col 13
#                                                 command to be executed starting in col 14
#
#
#   2. cronLOG.txt
#
#      As commands are executed they are copied to this file with the
#      timestamp when they were executed put in front.
#
#
# Temporary file:
#
#   1. cronOUT.txt - temporary file that becomes cronIN.txt
#
while true
do
exec 3< cronIN.txt
thisRun=`date +%Y%m%d%H%M`
echo $thisRun "fauxCron checking..."
read taskTime taskCommand <&3
while [ $taskTime != "<end>" ]
do
    if [ $taskTime -le $thisRun ]; then
      nice $taskCommand &
      echo $thisRun $taskTime $taskCommand >> cronLOG.txt
    else
      echo $taskTime $taskCommand >> cronOUT.txt
    fi
    read taskTime taskCommand <&3
done
echo "<end>" >> cronOUT.txt
exec 3>&-
rm -f cronIN.txt
cat cronOUT.txt > cronIN.txt
rm -f cronOUT.txt
sleep 5m
done
exit 0



clacker, having it run at a certain time got me thinking about another project I want to do. I've been playing around with having mplayer record internet radio broadcasts. It would be nice to be able to have my server start recording at a certain time and day. When I get it working I'll post how it's done.

The other nice thing is that you can modify cronIN.txt with additional tasks and not have to change the script. Now if someone would build a nice Flua gui frontend to populate the cronIN.txt file.... maybe with a "repeat" slider that would create x number of entries. One for each day, week, month.... on a given task.

Next Page...
original here.