Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (3) </ 1 [2] 3 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: scheduling a task, alternative to cron?< Next Oldest | Next Newest >
clacker Offline





Group: Members
Posts: 570
Joined: June 2004
Posted: Mar. 22 2006,00:58 QUOTE

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
Back to top
Profile PM 
rupert2 Offline





Group: Members
Posts: 2
Joined: Mar. 2006
Posted: Mar. 23 2006,11:47 QUOTE

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..
Back to top
Profile PM 
newOldUser Offline





Group: Members
Posts: 241
Joined: Oct. 2004
Posted: Mar. 24 2006,03:03 QUOTE

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 :)


--------------
======== Note =========
The DSL Forum Search default is 'OR'
If you search for "cat dog" you will get all "cat" posts and all "dog" posts.
If you search for "cat AND dog" you will only find results that contain BOTH "cat" and "dog"
Add '*' for wildcards, " cat* and dog* "
Back to top
Profile PM 
clacker Offline





Group: Members
Posts: 570
Joined: June 2004
Posted: Mar. 24 2006,13:02 QUOTE

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
Back to top
Profile PM 
newOldUser Offline





Group: Members
Posts: 241
Joined: Oct. 2004
Posted: Mar. 24 2006,17:22 QUOTE

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.


--------------
======== Note =========
The DSL Forum Search default is 'OR'
If you search for "cat dog" you will get all "cat" posts and all "dog" posts.
If you search for "cat AND dog" you will only find results that contain BOTH "cat" and "dog"
Add '*' for wildcards, " cat* and dog* "
Back to top
Profile PM 
11 replies since Mar. 19 2006,14:48 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (3) </ 1 [2] 3 >/
reply to topic new topic new poll
Quick Reply: scheduling a task

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code