LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   dcron - run script last friday of month (http://www.linuxquestions.org/questions/linux-software-2/dcron-run-script-last-friday-of-month-940112/)

cotton213 04-16-2012 04:55 PM

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.

TB0ne 04-16-2012 06:47 PM

Quote:

Originally Posted by cotton213 (Post 4654443)
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/

elucches 04-16-2012 06:48 PM

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.)

cotton213 04-17-2012 12:21 PM

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.

TB0ne 04-17-2012 01:10 PM

Quote:

Originally Posted by cotton213 (Post 4655262)
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.

elucches 04-18-2012 07:42 AM

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

Dafydd 05-21-2012 11:35 PM

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


MarkWW 06-25-2012 04:03 PM

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 07:10 AM.