You can schedule a cron job to run on the third Monday and every Thursday by using the 3-4 range in the day-of-week field for Mondays, combined with a wildcard for Thursdays. This approach leverages standard Linux cron syntax to target specific weeks within a month while maintaining a regular weekly schedule for other days.
Understanding Cron Syntax
The cron daemon uses a specific format to determine when commands should execute. A standard cron line consists of five time-and-date fields followed by the command to run. The fields are minute, hour, day of month, month, and day of week. Each field can contain specific values, ranges, or wildcards. For this task, we need to focus on the last two fields: day of month and day of week. While you might be tempted to use the day of month (1-31) to find the "third" Monday, this is unreliable because months vary in length and the position of Mondays changes. Instead, we use the day of week field. In Linux cron, days are numbered 0-7, where both 0 and 7 represent Sunday. Monday is 1, Tuesday is 2, Wednesday is 3, Thursday is 4, Friday is 5, Saturday is 6, and Sunday is 0 or 7.Scheduling for Every Thursday
Scheduling a job for every Thursday is straightforward. You simply set the day of week field to4. For example, if you want the job to run at midnight every Thursday, your entry would look like this:
0 0 * * 4 /path/to/your/script.sh
This tells the cron daemon to execute the script at minute 0, hour 0, regardless of the day of the month or month, but only when the day of the week is 4 (Thursday). This part of the configuration is static and does not require complex logic.
Scheduling for the Third Monday
Determining the "third" Monday requires a bit more nuance. The third Monday of any given month will always fall between the 15th and the 21st. Therefore, we can use a range in the day of week field. However, standard cron does not have a built-in "nth weekday" function that works across all months perfectly without additional logic. A common and effective workaround is to use the range3-4 in the day of week field if we were looking for the third or fourth occurrence, but since we specifically want the third Monday, we must consider that the third Monday is always a Monday. We cannot simply say "Monday of week 3" because cron weeks start on Sunday.
Instead, the most robust method for targeting the third Monday specifically is to use a list of possible dates in the day of month field, combined with a check for the correct day of week. However, a simpler interpretation often used in basic cron setups is to allow the job to run on any Monday that falls in the middle of the month. But wait, the prompt asks for "Monday of Week 3". In many contexts, "Week 3" implies the third week of the month.
Let's refine the approach. To hit the third Monday, you can actually use a combination of day of month and day of week. But the cleanest way in pure cron without external scripts is tricky. Let's look at the related article about scheduling batch jobs. Often, system administrators prefer to let the script itself handle the date logic if the cron syntax becomes too convoluted.
However, there is a clever trick. If you set the day of week to 1 (Monday) and the day of month to 15-21, you will catch the third Monday. Why? Because the third Monday must occur between the 15th and 21st. It cannot be earlier than the 15th (since the first Monday is at earliest the 1st, second is 8th, third is 15th) and it cannot be later than the 21st (since the fourth Monday is at latest the 21st).
So, the cron entry would be:
0 0 15-21 * 1 /path/to/your/script.sh
This ensures the script runs on Mondays that fall between the 15th and 21st of the month. Since only one Monday can fall in this window (the third Monday), this effectively targets the third Monday.
Combining Both Conditions
To run the job on both the third Monday and every Thursday, you can combine these conditions using a comma-separated list in the day of week field, but you must handle the day of month constraint carefully. If you use0 0 * * 1,4, it runs every Monday and every Thursday. This is not what we want. We want only the third Monday.
One solution is to create two separate cron entries. One entry handles the third Monday, and another handles every Thursday. This is the cleanest and most maintainable approach.
Entry 1 (Third Monday):
0 0 15-21 * 1 /path/to/your/script.sh
Entry 2 (Every Thursday):
0 0 * * 4 /path/to/your/script.sh
By splitting them, you avoid complex logical errors in the cron expression. If you prefer a single entry, you might try:
0 0 15-21 * 1
0 0 * * 4
But most cron daemons do not support multi-line entries in a single block easily without duplication. So, two lines in the crontab file is the standard practice.
Testing Your Configuration
After adding these lines to your crontab (viacrontab -e), it is crucial to verify they work as expected. You can simulate the next run time using tools like ncal or online cron calculators. Check that the third Monday of the current month falls within the 15-21 range. Also, ensure that the path to your script is absolute and executable.
For more advanced scheduling needs, such as handling timezone differences or complex dependencies, consider exploring Scheduling and Managing Batch Jobs with atd on Linux. This tool complements cron for one-off tasks.
Frequently Asked Questions
What happens if the month has five Mondays?
The range 15-21 will still only match the third Monday. The fifth Monday will fall after the 21st, so it will be ignored by this specific cron rule. If you needed the fifth Monday, you would adjust the range to 22-28.Can I use weekdays instead of numbers?
Yes, most modern cron implementations accept abbreviations likeMON or THU instead of numbers 1 and 4. This makes the crontab entry more readable for human editors, though the underlying logic remains identical.
Why not just use the day of month 15?
Using day of month 15 alone is incorrect because the 15th is not always a Monday. It could be a Tuesday, Wednesday, etc. Combining the day of month range with the day of week ensures you only trigger on the correct day.How do I edit my crontab safely?
Always usecrontab -e to edit your scheduled tasks. This opens the editor safely and validates the syntax before saving. Avoid editing the /var/spool/cron files directly, as this can lead to permission issues or syntax errors that prevent the cron daemon from loading your jobs.