LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-15-2007, 02:18 PM   #1
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Rep: Reputation: 30
Running a job the first monday of the month?


I looking for a way to start a job the first monday of the month at 7h.

I've read the crontab's man page and it seems it can't be done with cron.

I'm not real good with scripts, could someone point me in the right direction?

I guess I could start the job the first thru the 7th day of every month and somehow test the date to see if we're monday...

Anyway, any pointers would be appreciated.


Yanik
 
Old 05-15-2007, 03:02 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Something like this in the script called by cron, located in the 'cron.foldername' folder (I'm not great with scripting either, but I think this would work):
Code:
#!/bin/sh
weekday=`date +%A`
if [ $weekday == "Monday" ]; then
  < insert job here >
fi
and the cron would be run as you figured: for the first 7 days each month, by adding this to the root crontab (or whoever's crontab it is) like:
Code:
00 7 1,2,3,4,5,6,7 * 0,1,2,3,4,5,6 /usr/bin/run-parts /etc/cron.foldername 1> /dev/null
This runs the check/job each day of the week (0-6) for the first 7 days (1-7) of each month.

EDIT: Not sure about the '==' in BASH; it may need only one '=' ..

Last edited by GrapefruiTgirl; 05-15-2007 at 03:14 PM.
 
Old 05-15-2007, 03:34 PM   #3
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Original Poster
Rep: Reputation: 30
thanks GrapefruiTgirl,

I think I got a oneliner tho:

Code:
0 7 * * * [ `/bin/date +%u` -eq 1 -a `/bin/date +%e` -lt 8 ] && /some/command 2>&1 >/dev/null
So if we're monday AND the date is below the 8th, we're the first monday of the month for sure SO do some command.

thanks again
 
Old 05-15-2007, 04:07 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Hey that looks great and much more complicated looking than I would have come up with! So many options to the 'date' command does it work how you want?
 
Old 05-15-2007, 05:48 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
How about
0 7 1-7 * 1 /path/to/your/command
 
Old 05-15-2007, 06:05 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Dang Mickaelk.. That looks so darned simple, and yet.. It looks like just the thing lol.
 
Old 05-15-2007, 06:18 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
It should work.
 
Old 05-16-2007, 07:03 AM   #8
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Original Poster
Rep: Reputation: 30
yeah I thought about that one but it doesn't work, here's the relevent part of the man page:

Quote:
Note: The day of a command’s execution can be specified by two fields —
day of month, and day of week. If both fields are restricted (i.e.,
aren’t *), the command will be run when either field matches the cur‐
rent time. For example,
‘‘30 4 1,15 * 5’’ would cause a command to be run at 4:30 am on the 1st
and 15th of each month, plus every Friday.
 
Old 03-05-2008, 01:02 PM   #9
Pete89
Member
 
Registered: Jan 2007
Location: Granada Spain
Distribution: Ubuntu, Slackware, Debian, OpenWRT
Posts: 35

Rep: Reputation: 15
I am wondering if Yanik ever got this to work:

Code:
0 7 * * * [ `/bin/date +%u` -eq 1 -a `/bin/date +%e` -lt 8 ] && /some/command 2>&1 >/dev/null

I am looking to run a cron the first Monday of every month and would like to confirm this.


Thanks,



Pete
 
Old 03-05-2008, 01:34 PM   #10
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Original Poster
Rep: Reputation: 30
Here's what I have and that I can confirm it works.


[ `/bin/date +%u` -eq 1 -a `/bin/date +%e` -lt " 8" ] && /some/command 2>&1


I had to put it in a bash script and make cron execute it.
 
Old 03-05-2008, 03:12 PM   #11
Pete89
Member
 
Registered: Jan 2007
Location: Granada Spain
Distribution: Ubuntu, Slackware, Debian, OpenWRT
Posts: 35

Rep: Reputation: 15
OK but ....

So then you execute the script everyday then right? And if its Monday and a date less than 8 it will run the command right?

Sorry but I still dont what the actual content does.



Thanks a lot for your help ....


Pete
 
Old 03-05-2008, 04:12 PM   #12
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Original Poster
Rep: Reputation: 30
that's right
 
Old 03-05-2008, 05:08 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Indeed, here is another method I found. The following will run only on Mondays instead of everyday but the test is only true if it is the first week. Also easily modified for any day of any week.

0 7 * * 1 [ `date +\%e` -gt 0 -a `date +\%e` -lt 8 ] && some command
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Scheduling a CRON job to run on the first Sunday of the month New2Linux2 Linux - Software 10 11-08-2016 12:25 PM
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
cron job not running vincebs Linux - Software 34 10-30-2004 01:27 PM
Cron job to run every monday wednesday and friday...? init Linux - General 4 07-29-2004 11:24 AM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:33 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration