Find the answer to your Linux question:
Results 1 to 10 of 10
Hello, I want to execute cron on the first Sunday of every month. I have set the cron as : --------- 0 0 1-7 * 0 /command --------- But the ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7

    Monthly Cron

    Hello,

    I want to execute cron on the first Sunday of every month. I have set the cron as :
    ---------
    0 0 1-7 * 0 /command
    ---------
    But the problem is that above cron executes from 1st to 7th day of the month, instead of just running on SUNDAY.
    Any help will be appreciated.

    Regards,
    Eric

  2. #2
    Just Joined!
    Join Date
    Jul 2008
    Posts
    31
    Eric,

    The format of the command as you have it is correct. The answer given by Paul will run every Sunday morning at 1 AM, which is not what you want.

    My research indicates that some day of the month and day of the week parameters are programmed as AND and some as OR. AND is correct. Looks like you discovered one of the versions set for OR.

    You could fake it out using a semaphore, if you wish. Set up a "month" file, check it for the current month when the script starts. If so, exit out. If not, write the current month into the file (to keep it from executing until next month) and continue the script. Crontab entry becomes:

    0 0 * * 0 /command

    Which means the script will attempt to execute every Sunday, but the semaphore will halt all except the first one in each month. It's kludgy, but if it looks stupid but it works, it's not stupid.

  3. #3
    Just Joined!
    Join Date
    Jan 2005
    Posts
    2
    Yep - Sri. I need more sleep!

    You could write a script that would first check if it is the first Sunday and, if so, perform your command. Die otherwise.

    Paul.

  4. #4
    Just Joined!
    Join Date
    Apr 2010
    Posts
    69
    Eric,

    You might also run your command with a test (&&) to check that the current day is less than or equal to the seventh day of the month. Alternatively, if you are looking into complex scheduling, you might check out jobscheduler dot sourceforge dot net (can't post links yet).

    - N

  5. #5
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    623
    Quote Originally Posted by eric85 View Post
    Hello,

    I want to execute cron on the first Sunday of every month. I have set the cron as :
    ---------
    0 0 1-7 * 0 /command
    ---------
    But the problem is that above cron executes from 1st to 7th day of the month, instead of just running on SUNDAY.
    Any help will be appreciated.

    Regards,
    Eric
    0 0 1-7 * * [ "$(date '+%a')" == "Sun" ] && command
    should work.

  6. #6
    Just Joined!
    Join Date
    Jul 2008
    Posts
    81
    Quote Originally Posted by Toadbrooks View Post
    Eric,

    The format of the command as you have it is correct. The answer given by Paul will run every Sunday morning at 1 AM, which is not what you want.

    My research indicates that some day of the month and day of the week parameters are programmed as AND and some as OR. AND is correct. Looks like you discovered one of the versions set for OR.
    From the beginning, the form of crontab(5) has been the same. The field "day of month" and the field "day of week" are ORed together. Not a difference in versions of cron. Other specification fields "minute" "hour" "month" are ANDed with the "day of month OR day of week" fields.

    Perhaps it should have been different, but it isn't. You have come across the most common misconception in the use of cron.

  7. #7
    Just Joined!
    Join Date
    Jul 2008
    Posts
    31
    Like I said, I've been able to find examples both ways. There's a sample which indicates AND works at

    www pantz org/software/cron/croninfo html

    (Dots missing because I haven't done 15 posts yet. The example is doing 3rd Monday.) I'm not saying it's correct, just the quickest one I could find.

    In any case, I started doing SunOS back in 1990, and it always worked as AND. I have no doubt that you are correct for the version you use. But if your assertion is that AND is a misconception and it's never been that way, then a lot of us have been confused for more than 20 years.

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    From the "pantz" link:

    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 current 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.
    That looks like "OR" to me - if either field matches (day of month OR day of week), the command is run.

  9. #9
    Just Joined!
    Join Date
    Jul 2008
    Posts
    81
    The version I use, and the one that I cite, is the one that was written by Paul Vixie in 1987. It seems to be the standard, although there have been minor alterations over the years. You can go read the source code if you wish, you will find a discussion there about the OR nature of the day-of-week and day-of-month specification.

    I suspect that the example you cite at <www.pants.org> is one that "seems to be correct" but was never actually tried.

  10. #10
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7
    0 0 1-7 * * [ "$(date '+%a')" == "Sun" ] && command
    should work.
    This is working for me
    Thank very much for all of you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...