You are here:  » Quick CRON question


Quick CRON question

Submitted by madstock on Sat, 2006-10-07 09:03 in

Currently, thanks to a previous answer from David, my CRON job contains lots of lines such as:

if date | grep Mon; then
<STUFF THAT IS DONE ON MONDAY>
fi
if date | grep Mon; then
<STUFF THAT IS DONE ON TUESDAY>
fi
etc..

Would it be possible to instead get it to do different actions on each day by date? E.g. different jobs on 1st, 2nd, 3rd of the month etc?

Looking around the interweb it appears that I may need to use %d within the variable (?), however I don't want to stick just any-old combination in without it being verified, as I am not sure that if I did something like:

if date %d | grep 1; then
<STUFF TO DO>
fi

Whether this would do on the 1st of the month, or any date during the month that contains a 1? (e.g. 10th, 11th, 21st etc.).

Sorry if I am making this more complicated than it needs to be, but I really don't want to trash the server by running about 1800 processes at once.

Thanks in advance,
Duncan

Submitted by support on Sat, 2006-10-07 09:48

Grep could get tricky for the reason you mention - it would match any day containing 1 rather than just the 1st. Probably easiest to stick the day into a variable and use an if statement to enclose any actions you want on a given day....

day=`date +%d`
day=`expr $day`
if [ $day -eq 1 ]; then
  echo "Do 1st of the month stuff"
fi
if [ $day -eq 2 ]; then
  echo "Do 2nd of the month stuff"
fi
#etc. etc.

You don't need an entry for any days that you don't want to do anything....

Cheers,
David.

Submitted by madstock on Sat, 2006-10-07 10:26

Thanks for that - I assume that I don't have to use echo statements for the "stuff" within the .sh file? (imports, automated downloads etc.)?

E.g - to download, gunzip and import a file on the 26th of the month:

day=`date +%d`
day=`expr $day`
if [ $day -eq 1 ]; then
  <do stuff on 1st>
fi
etc, until...
if [ $day -eq 26 ]; then
php /path/to/downloadfeedscript.php
php /path/to/gunzipscript.php
php /path/to/scripts/import.php @MODIFIED
fi

Thanks again,
Duncan
specialoffers.at

Submitted by support on Sat, 2006-10-07 10:29

Yeah no need for the echo statement, just enter the commands you need (although there has to be _something_ otherwise the script will fail with an unexpected 'fi')....

Cheers,
David.

Submitted by madstock on Sat, 2006-10-07 19:42

Thanks again, David.

Submitted by support on Sat, 2006-10-07 20:01

It made perfect sense! You're correct - just put stuff you want to do every day at the end of the script.

Cheers,
David.