Cron job first Sunday of the month

Solved
splintermik -  
 splintermik -
Hello everyone, here it is, I would like a command to execute every 1st Sunday of each month

So I put this in crontab -e

01 00 1-7 * 0 command

The problem is that the command runs every Sunday and every day from the 1st to the 7th

Is there another command that would allow it to run once, for the 1st Sunday of each month?

Thank you

3 answers

  1. HostOfSeraphim Posted messages 7340 Status Contributor 1 609
     
    Hello,

    An interesting solution:

    https://stackoverflow.com/questions/3241086/how-to-schedule-to-run-first-sunday-of-every-month

    00 09 * * 7 [ $(date +\%d) -le 07 ] && /run/your/script

    "The date +%d gives you the number of the current day and then you can check if the day is less than or equal to 7. If it is, then run your command.

    If you run this script only on Sundays, it should mean that it runs only on the first Sunday of the month."
    1
    1. splintermik
       
      Thank you for your answer, so if I understand correctly, : [ $(date +\%d) -le 07 ] is used to see if the date is equal to or less than 7?

      EDIT: conclusive test, thank you very much!! :D
      0
    2. HostOfSeraphim Posted messages 7340 Status Contributor 1 609
       
      It is used to know the day number when the task is executed. If you configure the task to run every Sunday, if the result is less than or equal to 7, it means it's the first Sunday of the month. Otherwise, it means there has already been a Sunday in that month.
      0
  2. HostOfSeraphim Posted messages 7340 Status Contributor 1 609
     
    Another solution, a bit less clean (even messy):

    You initialize a file 'premierdimanche' to 0.

    You run your script every Sunday and check the content of this file:

    • if it is 0: you run the script content and set the file content to 1.
    • if it is 1: your script has already been run this month.

    Then, you add a task on the 1st of the month to reset this file to 0.
    1
    1. splintermik
       
      hum, yeah, but in the end the first solution is the simplest ^^
      0
  3. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
     
    Hi,

    It's impossible to directly set the first Sunday of the month in the crontab settings.

    However, it's possible to work around it, as explained here.

    --
    ☯ Zen my nuggets ☮
    Do something for the environment, close your windows and adopt a penguin. <('')
    0
    1. splintermik
       
      Thank you for your response

      I just tested with at, so here is the command

      at now +1 minutes /etc/blablabla

      I got back

      syntax error. Last token seen: /
      Garbled time

      So I tried: at now +1 minutes etc/blablabla

      and I got:

      syntax error. Last token seen: e
      Garbled time

      I don't really understand how this works, if it's a command that can only be executed via the console, it isn't very useful for running automatically every first Sunday of the month :p
      0
      1. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501 > splintermik
         
        echo 'echo "Hello" > /tmp/file.txt' | at now +1 minutes
        0