11

How to make a script run at e.g. 16:00 every last thursday of the month using gnome-schedule or cron?

user73331
  • 901
  • 4
  • 14
  • 23
  • 0 16 * * 4 test $(date +\%m) -ne $(date -d 7days +\%m) && export DISPLAY=:0 ; scriptfile – Kevin Jan 23 '18 at 18:25

2 Answers2

3

Open crontab (crontab -e)and add this entry.

0 16 24-31 * 4 script_file

You can use this online cron generator.

Hope this helps.

devav2
  • 35,738
  • 17
  • 79
  • 82
  • Or use [this editor](http://www.corntab.com/pages/crontab-gui). It's a bit more comprehensive. – Glutanimate Nov 09 '12 at 04:20
  • 1
    great, thanks. But it won't run on april 2014 :) – user73331 Nov 09 '12 at 04:27
  • changing it to 24-31 will solve it :) – devav2 Nov 09 '12 at 05:50
  • but then.. look at july 2014 :) Sorry, just kidding.. The question is solved. Thanks again. – user73331 Nov 09 '12 at 07:20
  • :) glad that you solved the issue. – devav2 Nov 09 '12 at 07:22
  • btw if the pc is off will this job work when I switch it on? Will anacron perform this task? – user73331 Nov 09 '12 at 11:42
  • Cron doesn't run when the system is switched off. Anacron can perform this task where your system doesn't need to run like a server 24x7 – devav2 Nov 09 '12 at 14:44
  • In July 2014, wouldn't this run twice, on Thu 24th and Thu 31st? – deed02392 Apr 15 '16 at 12:53
  • 9
    From `man 8 crontab`: __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.__ So won't this also be run every Thursday? – Dan Gravell Oct 10 '16 at 10:32
  • When last day of month is Wednesday February 28th I think this script breaks. – WinEunuuchs2Unix Jan 26 '17 at 18:11
  • 7
    This is completely wrong. This will run on the 24th, 25th,...,31th **and** every Thursday. See the comment of @DanGravell or [IEEE Std 1003.1](http://pubs.opengroup.org/onlinepubs/007904975/utilities/crontab.html) – miracle173 Aug 10 '17 at 05:36
  • Warning: [crontab.guru](https://crontab.guru/#0_16_24-31_*_4) says that this will run "At 16:00 on every day-of-month from 24 through 31 AND on Thursday." (as others have already said) – Tom Mar 26 '21 at 07:18
2

Faced with the same issue I created a script (cron-last-sunday) to help with this.

Here is a brief example of how easy you can achieve these "peculiar" requirements.

# every first saturday
30 6 * * 6 root run-if-today 1 && /root/myscript.sh

# every last sunday
30 6 * * 7 root run-if-today L && /root/myscript.sh

# every third tuesday
30 6 * * 2 root run-if-today 3 && /root/myscript.sh
MGP
  • 121
  • 3