LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-16-2012, 04:55 PM   #1
cotton213
LQ Newbie
 
Registered: Dec 2004
Location: Michigan
Distribution: Slackware, Red Hat, Ubuntu
Posts: 23

Rep: Reputation: 1
dcron - run script last friday of month


I am running Slackware 13.37.0. I have read the discussion here: http://www.linuxquestions.org/questi...script-869370/

I cannot seem to get the syntax for running a script on the last (or first or third, whatever) friday (or monday, etc.) of the month to work. I have put entries in /etc/cron.d (it's what I'm used to) and in root's crontab (using crontab -e). The entries just seem to be ignored, even after killing and restarting crond.

Here is what my entry looks like in /var/spool/cron/crontabs/root:

Code:
* * 3 * mon  /bin/date > /tmp/barbout 2>&1
This should just write out the date to a file every minute on the third monday of the month (today). Can anyone tell me what I'm doing wrong? According to the man page, this should be simple and I'm just failing completely!

Thanks for any help.
 
Old 04-16-2012, 06:47 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
Quote:
Originally Posted by cotton213 View Post
I am running Slackware 13.37.0. I have read the discussion here:

I cannot seem to get the syntax for running a script on the last (or first or third, whatever) friday (or monday, etc.) of the month to work. I have put entries in /etc/cron.d (it's what I'm used to) and in root's crontab (using crontab -e). The entries just seem to be ignored, even after killing and restarting crond. Here is what my entry looks like in /var/spool/cron/crontabs/root:

Code:
* * 3 * mon  /bin/date > /tmp/barbout 2>&1
This should just write out the date to a file every minute on the third monday of the month (today). Can anyone tell me what I'm doing wrong? According to the man page, this should be simple and I'm just failing completely!
Well, your day-of-week should be a number, not "mon". A good example is:
http://www.pantz.org/software/cron/croninfo.html

There are other threads here dealing with getting cron to be more granular, but this:
Code:
1 * * * 5 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && /path/to/your/script
..will run it on every last Friday (or whatever weekday) of the month. Modify the date command to be whatever you'd like. An explanation:
  • $(date +"\%m") -ne $(date -d 7days +"\%m") - Checks to see if the month today is not equal to month next week (7days from now - same day). If they are equal then current day (Friday in our case) is not the last weekday (we specified Friday) of the month. If not, it is the last Friday. So...
  • Run your script /path/to/your/script
Another example is here:
https://www.linuxquestions.org/quest...-month-619400/
 
1 members found this post helpful.
Old 04-16-2012, 06:48 PM   #3
elucches
Member
 
Registered: Jan 2011
Posts: 108

Rep: Reputation: 11
Try with the following format:
minute (0-59),
hour (0-23),
day of month (1-31),
month of the year (1-12),
day of the week (0-6 with 0=Sunday),
command
(putting all fields above in the same line as you've done.)
 
Old 04-17-2012, 12:21 PM   #4
cotton213
LQ Newbie
 
Registered: Dec 2004
Location: Michigan
Distribution: Slackware, Red Hat, Ubuntu
Posts: 23

Original Poster
Rep: Reputation: 1
Thanks elucches. I tried changing my "mon" to 1 (ok, since today is Tuesday I changed it to 2) -- still no luck.

The man for crontab (dillon's lightweight cron daemon) specifically allows jan-dec and sun-sat and also states:
Quote:
If you specify both a day in the month and a day of week, it will be interpreted as the Nth
such day in the month.
with the example:
Quote:
To request the last Monday, etc. in a month, ask for the "5th" one. This will always match the last Mon-
day, etc., even if there are only four Mondays in the month:

# run at 11 am on the first and last Mon, Tue, Wed of each month
0 11 1,5 * mon-wed date

When the fourth Monday in a month is the last, it will match against both the "4th" and the "5th" (it will
only run once if both are specified).
Hence my attempt.

I would still like to know why this doesn't seem to work as advertised, at least for me.

However, thank you very much, TB0one, for the script. This will solve my specific "last Friday of the month" problem nicely. Thanks also for the other reference -- very helpful.

Thanks again everyone.
 
Old 04-17-2012, 01:10 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
Quote:
Originally Posted by cotton213 View Post
Thanks elucches. I tried changing my "mon" to 1 (ok, since today is Tuesday I changed it to 2) -- still no luck.
The man for crontab (dillon's lightweight cron daemon) specifically allows jan-dec and sun-sat and also states:
I would still like to know why this doesn't seem to work as advertised, at least for me.
Not sure...personally, I have never put abbreviated days into cron.
Quote:
However, thank you very much, TB0one, for the script. This will solve my specific "last Friday of the month" problem nicely. Thanks also for the other reference -- very helpful.
Thanks again everyone.
You're welcome. You can modify the date strings as needed, and test them from the command line with
Code:
echo $(date +"%m") -ne $(date -d 7days +"%m")
..until you get it like you want. Essentially, that's a little bit of shell-script, and will echo something like
Code:
04 -ne 04
..indicating that the month TODAY is going to be the same a week from today. The "-ne" operator is "not equal". Play with the date strings until you figure out the logic correctly.
 
1 members found this post helpful.
Old 04-18-2012, 07:42 AM   #6
elucches
Member
 
Registered: Jan 2011
Posts: 108

Rep: Reputation: 11
Hi cotton,
Well I have to admit that I had no luck trying to run a job this third wednesday of the month, even using numbers or specifying hour and minute. I have no idea why. I'm sorry for misleading you.
I hope we find the answer.
Best regards,
Esteban
 
Old 05-21-2012, 11:35 PM   #7
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
I need to send an email on the 3rd Monday of each month. I used brute force to to it.

crontab entry
Code:
30 18 * * 2 ~/bin/third_tues_fly_tying.sh
third_tues_fly_tying.sh
Code:
#!/bin/bash
whichmonth=$(date | awk -F" " '{print$2}')
if [ $whichmonth = 'Jan' ]; then
	whichTues=14;
elif [ $whichmonth = 'Feb' ]; then
	whichTues=18;
elif [ $whichmonth = 'Mar' ]; then
	whichTues=18;
elif [ $whichmonth = 'Apr' ]; then
	whichTues=15;
elif [ $whichmonth = 'May' ]; then
	whichTues=21;
elif [ $whichmonth = 'Jun' ]; then
	whichTues=18;
elif [ $whichmonth = 'Jul' ]; then
	whichTues=16;
elif [ $whichmonth = 'Aug' ]; then
	whichTues=20;
elif [ $whichmonth = 'Sep' ]; then
	whichTues=17;
elif [ $whichmonth = 'Oct' ]; then
	whichTues=15;
elif [ $whichmonth = 'Nov']; then
	whichTues=19;
elif [ $whichmonth = 'Dec']; then
	whichTues=17;
fi
thisTues=$(date | awk -F" " '{print$3}')
if [ "$thisTues" = "$whichTues" ]; then
	echo "send email";
	cd /home/dave/tffrobot/
	./tuesday-flytying-send.sh
	exit 0
fi
	echo "not today";
exit 0

Last edited by Dafydd; 05-21-2012 at 11:38 PM.
 
Old 06-25-2012, 04:03 PM   #8
MarkWW
LQ Newbie
 
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
I know it doesn't help with the original question, but this may help with the brute force workaround.

It looks like the third Monday (or Tuesday) is the Monday(Tuesday) which falls in the days 15th-21st (inclusive). So if you check that it is Monday AND that it is in the 15-21 range, it's the third Monday of the month. That way, you don't need to go in every year and update the dates in your code to match the year's calendar.

That should work for every weekday. The third Friday should fall in the 15th-21st range. As should the Sunday, etc.
-Mark
 
  


Reply


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
Configure cron entry to run every other Friday? MarkEHansen Linux - General 1 10-14-2010 03:13 PM
[SOLVED] Going LOOPY on a Friday - Script Help Plz fusion1275 Programming 6 08-28-2009 07:19 AM
How can I set Plesk Crontab to only run a job on Friday? gogoskiracer Linux - Newbie 10 05-16-2009 01:57 AM
Cron Job to run on 2nd and 4th Friday of each month rust8y Linux - General 1 06-25-2007 09:07 PM
Cron job to run every monday wednesday and friday...? init Linux - General 4 07-29-2004 11:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:55 PM.

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