LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-16-2008, 06:18 AM   #1
joerilinux
LQ Newbie
 
Registered: Aug 2008
Posts: 2

Rep: Reputation: 0
Question 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?!

Last edited by joerilinux; 08-16-2008 at 06:55 AM.
 
Old 08-16-2008, 08:21 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by joerilinux View Post
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.

Last edited by colucix; 08-16-2008 at 08:27 AM.
 
Old 08-16-2008, 08:59 AM   #3
joerilinux
LQ Newbie
 
Registered: Aug 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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

Last edited by joerilinux; 08-16-2008 at 09:18 AM.
 
Old 09-17-2009, 11:26 PM   #4
babinu
LQ Newbie
 
Registered: Aug 2006
Posts: 1

Rep: Reputation: 0
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?
 
Old 09-18-2009, 03:08 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Hi babinu and welcome to LinuxQuestions!

Quote:
Originally Posted by babinu View Post
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.

Last edited by colucix; 09-18-2009 at 03:31 AM. Reason: Spell checking
 
  


Reply

Tags
cronjob, crontab


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Celebrate software freedom this Saturday LXer Syndicated Linux News 0 09-13-2007 02:41 PM
LXer: KDE-EDU 4.0 Polishing on Saturday LXer Syndicated Linux News 0 09-12-2007 11:30 AM
LXer: GPL 3: Coming Saturday? LXer Syndicated Linux News 1 03-20-2007 11:23 PM
cron: every 3 weeks on a saturday rgiggs Slackware 19 09-26-2005 04:59 PM
On the lighter side of Saturday morning... JordanH General 5 05-01-2005 12:11 AM


All times are GMT -5. The time now is 06:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration