-   Linux - Newbie (
-   -   Cronjob - Monthly on Saturday (

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


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

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

Then your crontab will be simply:

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

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

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


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


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)

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

And cron:

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!


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:

30 9 * * 6 /path/to/
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:

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

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:19 PM.