LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cronjob - Monthly on Saturday (http://www.linuxquestions.org/questions/linux-newbie-8/cronjob-monthly-on-saturday-663190/)

joerilinux 08-16-2008 06:18 AM

Cronjob - Monthly on Saturday
 
Been reading examples and been searching this forum, but nowhere is stated how to run a cronjob on a specific day of the week - monthly!

I want to run the script every 1st saturday of the month at 7.30am.

I tried this one:
30 7 1-7 * 6

Which would bound the script only to send it on the dates 1-7 of the month (I thought), but today 16th, the script was sending my mails...

Another option I can think of would be:
30 7 * */1 6 or a combination 30 7 1-7 */1 6

Where */1 would mean once per month (right?!) but not sure or that would work... Any Cron Guru out there?! :)

colucix 08-16-2008 08:21 AM

Quote:

Originally Posted by joerilinux (Post 3249541)
Where */1 would mean once per month (right?!)

Wrong. It is a timestep. For example */2 means "every two" minutes, hours or days depending in which field it appears. The */1 means "every timestep" and is equal to *.

Your problem may be solved by adding a check at the beginning of your script (if any) or wrap the command you want to run in a script. For example
Code:

#!/bin/bash
if [ $(date +%w) -eq 6 -a $(date +%d) -le 7 ]
then
  <your commands here>
else
  <other command here>
  <e.g. echo to log>
  exit 1
fi

Then your crontab will be simply:
Code:

30 7 * * * /path/to/script.sh
Hope this helps.

Edit: take in mind that crontab fields are addictive, that is if you specify
Code:

30 7 1-7 * 6
the job will be executed at 7:30 on the first seven days of the month AND every saturday.

joerilinux 08-16-2008 08:59 AM

Quote:

Originally Posted by colucix (Post 3249605)
Edit: take in mind that crontab fields are addictive...

Thanks!

I went wrong with NOT knowing that crons are addictive. So I will go for the in-script solution and just run the script every saturday.

eg. (PHP script)
Code:

// Once a month, otherwise exit script
if ( date("j") > 7)
{
 exit;
}

And cron:
Code:

30 7 * * 6

babinu 09-17-2009 11:26 PM

How do you run cron every 2 saturdays...
 
I would like add a cron for running a job every 2 saturdays .....

How do you do that?

colucix 09-18-2009 03:08 AM

Hi babinu and welcome to LinuxQuestions!

Quote:

Originally Posted by babinu (Post 3688021)
I would like add a cron for running a job every 2 saturdays .....

How do you do that?

Again you cannot specify this using the crontab time specifications, hence in my opinion you are forced to put a check at the beginning of your script and embed all the code as a block inside a control statement.

Said that, you can simply run your job every Saturday:
Code:

30 9 * * 6 /path/to/script.sh
and decide whether to run on odd or even weeks of the year. Using the date command the script can retrieve the number of week of the year, then check the remainder of the division by two. In bash you can put it in a one-liner using arithmetic substitution with a nested command substitution, like this:
Code:

if [ $(($(date +%W) % 2)) -eq 1 ]
then
  echo "Odd week! Running..."
  <block of code here>
else
  echo "Even week! Do nothing..."
fi

Substitute 1 with 0 in the test expression if you want to run every even week.

A downside is on year changes. Last week is 53 and first week of the new year is odd as well, so that your job will run two consecutive weeks. To avoid that you can:

1. Add a more fined control on even or odd years and switch to even or odds weeks accordingly.

2. Use a totally different approach and create/delete a dummy file every time the script runs. It will check for the presence of the file and a) if the file is absent don't run and create it, b) if the file is present run and delete it. In other words you can use a file as a toggle (for example you can create/delete a hidden file in your home directory with a unique name).

Edit: I checked and on my system the last week of the year is 52, whereas the first week is 00. Anyway, this does not change the considerations above.


All times are GMT -5. The time now is 07:52 AM.