scheduling a task


Forum: Programming and Scripting
Topic: scheduling a task
started by: rupert2

Posted by rupert2 on Mar. 19 2006,14:48
I'm a little new to DSL/Linux but the other distros I've used before  had 'cron' or 'at' to schedule a task/script to be run.  I don't see that with DSL.  I'm running a frugal install with JWM desktop, is there some function in the DSL frugal install or the JWM desktop that I could use to trigger a script to be run at a set time and date?

I've read that some users have done an apt-get to install cron but I didn't think that would work with a frugal install. Am I wrong?

The other thing I was thinking about was creating (if I had any idea how to do it)   a script that uses a SQLite database and 'wakes up' every five minutes to see if something needs to be launched. Does anyone have this script in their backpocket? Might be a nice, small addition to the DSL base system.

Posted by mikshaw on Mar. 19 2006,16:40
You can install applications into frugal with apt-get or dpkg, but you will need to reinstall them every time your reboot.

One thing to be careful with when using apt-get is that it will often take liberties with the support files it wants to install.  Since these packages were built specifically for the more fully-stocked Debian, they may require certain versions  of some libraries and other programs that are not available in DSL.  In contrast, myDSL packages are made to work with what DSL already has as much as possible. If they are compiled specifically for DSL they may be compiled to work with DSL-specific libraries, and sometimes have features disabled in order to prevent the need to add more software.

Posted by _pathos on Mar. 20 2006,06:30
could this be done with a bash script?
Posted by newOldUser on Mar. 21 2006,03:41
not sure how a bash script could do it.... what would you do? read a text file with Time and Task information? Maybe with flua, don't really know what functions flua has available. I guess DSL does come with a 'Calendar' utility, maybe it could be modified ( a lot ) or used as a starting point. What's that SQLite book app written in?  

I never thought much about scheduled tasks but I can see how it would be nice to have it available.  Actually I thought DSL had something like 'cron' or 'at' in it already.

What's available as a UCI ?  I think there is Python and Tcl. They're not native to DSL but a UCI is pretty light on resources and Python and Tcl are robust languages.  

What would it really need to do?

1. Read a list of dates and tasks
2. Determine if any tasks with a date/time less then the current date/time have not yet been executed.
3. Execute the task
4. mark it as done, so you have an audit trail.
5. go back to #2
6. if no tasks remain go to sleep for a while.

If you want to get fancy:
- add a 4.5 step to create the 'next task' in a recurring tasks ( the every day at 3 am type of tasks ).
- make the list of times and tasks a sqlite table
- create a webpage or some gui interface to enter and view the task list

Sorry, not much help, but the more I think about this, the more I think it could be useful.  Of course maybe it's as easy as pie to install cron and this would just be an exercise in twidling with electrons.

Good luck.  Have you done any internet searches looking for scripts?

Posted by AwPhuch on Mar. 21 2006,23:55
If you are running a frugal w/ persistant home directory I believe the crontab would be in /home/dsl/etc/crontab I believe

Brian
AwPhuch

Posted by clacker on Mar. 22 2006,00:58
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

Posted by rupert2 on Mar. 23 2006,11:47
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..

Posted by newOldUser on Mar. 24 2006,03:03
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 :)

Posted by clacker on Mar. 24 2006,13:02
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

Posted by newOldUser on Mar. 24 2006,17:22
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.

Posted by newOldUser on Mar. 30 2006,00:18
Updated version of fauxCron script is posted at the DSL blogsite for < newOldUser >.  Just some minor changes.
Posted by newOldUser on May 31 2006,00:45
I've made some changes to the fauxCron script to do weekly repeatable tasks.  The updates can be found at: < http://www.damnsmalllinux.org/talk/node/292 >

Update: 24 June 2006

I didn't notice at first but the blog page is having trouble displaying the scripts. It removes bits and pieces of them. So I suggest that you go to the blog to read about it but then travel to newolduser.googlepages.com to copy the scripts.  There's a link in the blog that you can follow.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.