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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-16-2012, 04:55 PM
|
#1
|
LQ Newbie
Registered: Dec 2004
Location: Michigan
Distribution: Slackware, Red Hat, Ubuntu
Posts: 23
Rep:
|
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.
|
|
|
04-16-2012, 06:47 PM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,191
|
Quote:
Originally Posted by cotton213
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.
|
04-16-2012, 06:48 PM
|
#3
|
Member
Registered: Jan 2011
Posts: 108
Rep:
|
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.)
|
|
|
04-17-2012, 12:21 PM
|
#4
|
LQ Newbie
Registered: Dec 2004
Location: Michigan
Distribution: Slackware, Red Hat, Ubuntu
Posts: 23
Original Poster
Rep:
|
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.
|
|
|
04-17-2012, 01:10 PM
|
#5
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,191
|
Quote:
Originally Posted by cotton213
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
..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.
|
04-18-2012, 07:42 AM
|
#6
|
Member
Registered: Jan 2011
Posts: 108
Rep:
|
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
|
|
|
05-21-2012, 11:35 PM
|
#7
|
Member
Registered: Oct 2008
Posts: 344
Rep:
|
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.
|
|
|
06-25-2012, 04:03 PM
|
#8
|
LQ Newbie
Registered: Jun 2012
Posts: 1
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 05:48 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|