How to create cron job that is executing every 3 months? - cron

I am using Hangfire in ASP.NET Core for Cron (recurring) Jobs, and I need to create a job that runs every three months starting from a given start date.
So if the start date was 15-Nov-2019, it should run on 15-Nov-2019, 15-Feb-2020, 15-May-2020 and so on and so forth.
And I need it to run every 3 months forever.
So I tried the following cron expression for this: "0 0 15 11/3 ?" or "0 0 15 11/3 *"
But after testing it on this translating site, it tells me that it will run on the following dates:
2019-11-15
2020-11-15
2021-11-15
2022-11-15
2023-11-15
So, if that is true, then how to make it run every three months starting from 15-Nov-2019 as described above and keep running forever?

The month field in cron takes a number between 1 and 12; depending on the cron implementation used, you could use an explicit list for the month field:
0 0 15 2,5,8,11 *
or a range with a step:
0 0 15 2-12/3 *
crontab.guru seems to support a single value with a step as well, but the crontab man page doesn't mention this style, so it might or might not work:
0 0 15 2/3 *
If you want to be able to set this up more than three months before you want it to run for the first time, you have to manually check the date; in shell (using GNU date), you would do something like this:
0 0 15 2-12/3 * [ $(date +%%s) -gt $(date -d '2019-11-01' +%%s) ] && yourcommand
This compares the current date to November 1st, 2019; if it is greater than that, the command is run.

Simple solution is to use the following command:
0 0 15 */3 *
It is very straight forward.Here's the output for your satisfaction from crontab.guru website
output of cron job

Related

Cronjob every hour from 17 to 6

I want to run a job every hour between 5 pm to 6 am.This is what I have tried
0 17-6 * * * command
But this doesn't work.
How will i set cronjob for the above?
Should two cronjobs have to be configured?
Ranges have to be in increasing order. Instead of 17-6 you want 0-6,17-23.

run cron job between 00:00 - 00:02 - 04:00 - 23:59 on every hour

I want to run a cron job between 0:00am - 02:00am - 04:00am and 23:59am on every hour.
I want to know if this is the correct syntax.
0,0-59 0-2,4-23/1 * * *
Thanks!
No, your syntax is not correctly formatted.
You can use:
0 0 0/1,0-2 ? * *
This will run according to the following rules:
At second :00, at minute :00, every hour between 00am and 02am,
and every hour starting at 00am, of every day
You can check CRON syntax with an explanation at:Cron Expression Generator & Explainer.
Also, I think this site has a really good breakdown to help understand what each section of the CRON expression relates to.
Edit: I just noticed you had the second part about running at 23:59. For this you will need to set up a second CRON job:
0 59 23 * * ? *
Use Case: At 23:59:00pm every day

CRON expression for Azure Webjob

I am trying to run my Webjob in schedule manner and i need it to run it during weekdays between 7 AM - 6PM. I was able to do add the start time but couldn't figure out how i would tell it to stop running after 6PM.
This is what i got so far:
0 0 7 ? * MON,TUE,WED,THU,FRI *
Thanks
You can use the following expression. This will run hourly for hours 7 to 18 on weekdays
0 * 7-18 * 1-5
proof:
https://crontab.guru/#0_*_7-18_*_1-5
so you use range (int-int) to define a range in cron, in this example you need to use 7-18 to run from 7am to 6pm

crontab settings with start_time and interval

I have a linux cron job to run. I want to configure its setting. What I have is, start_time for the job and interval after which it should repeat every time. interval is integer and has unit of day. So for example, I want to set up cron job starting on some random date in future and want to run that job periodically after every interval days. I tried to do 0 0 * * */interval but it does not give what I want. Any idea how to achieve it?
I think you may want something like
0 0 */interval * * /your/command
Basically switching day of week for day of the month. As for the random start date, that will have to be done somewhere else I think, like with a shell script which edits the cron file at a certain point etc.
EDIT:
This little script would allow you to edit the cron file.
#!/bin/sh
crontab -l > tempcron
echo "00 00 * * * /bin/ls" >> tempcron #just an example cron
crontab tempcron
rm tempcron

I need help using cronjob

I'm trying to write a cron job to run automatically a PHP file after 30 days. The PHP file has write permission 777.
Here is my code:
* * * */30 * php -f /var/www/virtual/my_domain_name.com/htdocs/./file.php > /dev/null 2>&1
But this is not working, I got no errors.
When i try:
* * * * * php -f /var/www/virtual/my_domain_name.com/htdocs/./file.php > /dev/null 2>&1
The script works, then it executes the file every second from.
Any ideas?
Putting */30 in the 4th field would cause the job to run only in the 30th month of the year, and every 30 months thereafter during the year -- i.e., never.
Putting */30 in the 3rd field (day of month) would cause it to run on the 30th day of each month (and on the 60th, 90th, ... day of the month if there were such a thing). And given the *s in the other fields, it would run once a minute on that day -- and never in February. I doubt that that's what you want.
If you want a job to run once a month, that's easy:
0 0 12 * * php ...
This will run the job at midnight on the 12th day of each month. Adjust the first two fields to pick a different time and the third to pick a different day.
There is no syntax for running a job once every 30 days. If that's really what you want, you can schedule the job to run once every day:
0 0 * * * php ...
and then have the job itself determine whether the current day is a multiple of 30.

Resources