How to schedule a cron for the first Thursday of every month
the answer:
0 15 * * 4 [ $(date +\%d) -le 7 ] && command
the explanation:
First of all you need to realize that the first Thursday of the month will always have a day of the month that is in the set {1,2,3,4,5,6,7}
. That is simply because there are only 7 days in a week. And thus the first Thursday cannot be have a day that is bigger then 7. The same holds evidently for any other weekday such as Tuesday or Saturday.
The command that cron will execute consists of two parts:
[ $(date "+\%d") -le 7 ]
: This is a simple test that checks if the day of the month is smaller than 7. It essentially checks if we are in the first week of the month. The commanddate -d "+%d"
returns the day of the month as a two-digit number (01,02,03,...,31
) and is a string. Thetest
command, here written in an alternative form with square brackets[ ... ]
does a check to see if the integer comming fromdate
is smaller then or equal to 7. You can read more on this when typingman test
in a terminal.command
: this is the command that is going to be executed if the previoustest
is successful. I.e. if we are in the first week of the month. The reason why this will only execute if thetest
command is successful, is because of the&&
. A command of the formcmd1 && cmd2
will executecmd1
and only when that one is successful will it executecmd2
.
So since we now have the command, we can now build our crontab file. With a bit more comments (parts starting with #
, you can write it as:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
0 15 * * 4 [ $(date +\%d) -le 07 ] && command
The above means, execute the above command at minute 0 of the 15th hour of any Thursday. And since the command contains the test for the first week, it will only run on the first Thursday of the month.
schedule a cron job to run on 1st, 2nd, and 3rd tuesday of every month
An alternative would be to create main-script to run every Tuesday, verify that Tuesday is and that hour is accordingly invokes the appropriate secondary script.
0 7,9 * * 2 wget -q -O /dev/null http://site.com/main-script
I hope that is helpful.
Greetings.
How to schedule to run first Sunday of every month
You can put something like this in the crontab
file:
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, 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.
Remember that in the crontab
file, the formatting options for the date
command should be escaped.
Crontab for every Thursday
the last asterisk should be replaced with 4 (day of week - Thursday) as following: 55 23 * * 4.
u can also use the following tool which makes your life easier when setting up a new crobjob - https://crontab.guru/
Cron expression for running rake task first Thursday of every month
I'm not sure there is a way with cron.
'20 8 1-7 * 4' means the first seven days of the month and on thursdays. Instead perhaps do it the first seven days of the month and in rails check if its thursday:
every '20 8 1-7 * *' do
rake 'data_import:check_for_presence_of_file' if Date.today.thursday?
end
Schedule Cron to skip first Saturday of every month
Since there seem to be no option in Cron.
I'm going with script for the above need.
**if [ $(date +%d) -le 7 ] && [ $(date +%u) -eq 1 ] ;**
[ $(date +%d) -le 7 ]
- Checks if the days fall in first 7 days of month
[ $(date +%u) -eq 6 ]
- Checks if day is equal to Saturday
Thanks.
Related Topics
"Relocation R_X86_64_32S Against '.Bss' Can Not Be Used When Making a Shared Object"
Numa-Aware Named Shared Memory for Linux
How to Check That Smart Card Is Working on Linux
Understanding Glibc Malloc Trimming
Process Control Block in Linux
Julia: System Image File "Sys.Ji" Not Found
List of Available Wireless Networks with Golang (Under Linux)
Gradle 1.3: Build.Gradle Not Building Classes
How to Increase The Size of Ephemeral Storage in a Kubernetes Worker Node
Pipe Tar Extract into Tar Create
Unix Command to Convert Xls File into Xlsx File
Embedded Linux Poll() Returns Constantly
Why Pid of a Process Is Represented by Opaque Data Type
Kaggle API Issue "Could Not Find Kaggle.JSON. Make Sure It's Located In......"
Home Directory Is Not Created with Adding User Resource with Chef