Crontab every hour from 7 am to 12 pm - linux

I need to run script every hour from 7 am to midnight (including midnight).
I've created this crontab but it didn't do anything at 7 and 8 am.
0 7-0/1 * * * ~/.venvs/p/bin/python ~/p/manage.py post_from_queue >> ~/p/logs/posts.log
Do you know where is the problem?

The proper format is :
0 0,7-23 * * * ~/.venvs/p/bin/python ~/p/manage.py post_from_queue >> ~/p/logs/posts.log
If this do not work on your case you should set two cron records:
0 7-23 * * * ~/.venvs/p/bin/python ~/p/manage.py post_from_queue >> ~/p/logs/posts.log
0 0 * * * ~/.venvs/p/bin/python ~/p/manage.py post_from_queue >> ~/p/logs/posts.log
It is wise to create script with the command above. And use absolute paths instead of relative

Related

string new line with special charactoers is not working what I exepted

I just created a string such as
str = "TZ=Europe/Berlin\n* * 1-5\n0 5 * * *\n"
I exepted that
TZ=Europe/Berlin
* * 1-5
0 5 * * *
in jenkins cron
but it was not working
any solutions?
Your chron expression doesn't appear valid. The validation error is "Day of month values must be between 1 and 31". Check it here: https://www.freeformatter.com/cron-expression-generator-quartz.html
Also check out https://plugins.jenkins.io/parameterized-scheduler/ for Jenkins specific help.

I want to send only 2k mail in 1 day mautic

I want to send 2k mail in one day. My Segment has more than 50k contacts. I tried to update the limits and max contact in Corn job. But it is not working.
Here are the Cron I setted up :
1 */4 * * * /home/ubuntu/backup.sh > /home/ubuntu/backup.log 2>&1
*/5 * * * * /var/www/html/bin/console mautic:segments:update --force --batch-limit=2000 --max-contacts=2000
*/10 * * * * /var/www/html/bin/console mautic:campaigns:update --force --batch-limit=2000 --max-contacts=2000
28 6 * * * /var/www/html/bin/console mautic:campaigns:trigger --force --batch-limit=2000 --max-events=2000
28 6 * * * /var/www/html/bin/console mautic:emails:send
*/1 * * * * /var/www/html/bin/console mautic:import
Can someone help on this, as I check that the limits and max contact is not been supported by mautic in new versions..
Over here you should use a combination of both cronjob setup and also define that Emails are going out through a que and not immediately.
If you go to the Cog (Settings) on the top right of Mautic, -> Configuration -> Email Settings, on the right panel you will see the Mail Sent Settings. Look for "How should email be handled" and change it from "Send Immediately" to "Queue", then under that there is "Message limit for queue processing" and here you can define how many emails you want to send per the mautic:email:send cron is triggered.
So if you would like to only send out 2Kover an entire 24 hour period you could play around and do something like this:
Message Limit for queue processing = 3
Crontab: */2 * * * * php /var/www/mautic/bin/console mautic:email:send > /dev/null
This will do a blast of 3 emails every 2 minutes for a period of 24 hours.
So 3 * 30 * 24 = 2,160 emails in a period of 24 hours.

Cron expression to execute every 45 minutes and not 45th minute of the hour

I am trying to generate a cron expression which executes every 45 minutes.
I have created the following expression.
0 0/45 * 1/1 * ? *
But this expression fires every 45th minute of the hour.
Ex: 10:45,11:00,11:45,12:00 etc.
But can we generate an expression which fires for example,
10:45,11:30,12:15 etc
The Cron expression does not support for every 45th minute. You can use with Trigger:
Trigger trigger = TriggerBuilder
.newTrigger()
.startAt(startTime)
.withSchedule(
CalendarIntervalScheduleBuilder
.calendarIntervalSchedule()
.withIntervalInMinutes(45)
.withMisfireHandlingInstructionDoNothing())
.build();
You can't do that directly.
0,45 */3 * * * ? *
30 1,4,7,10,13,16,19,22 * * * ? *
15 2,5,8,11,14,17,20,23 * * * ? *

Run Cron Job every 45 minutes with Node-Cron

I'm using node-cron to run scheduled jobs.
I want the jobs to run every 45 minutes, but its acting strangely
Here's the pattern I'm using
'00 */45 * * * *'
I started my script at
Tue Jun 17 2014 08:17:39 GMT+0000 (GMT)
Here's are the first couple of times the job was executed
1. Tue Jun 17 2014 08:45:03 GMT+0000 (GMT)
2. Tue Jun 17 2014 09:00:01 GMT+0000 (GMT)
3. Tue Jun 17 2014 09:45:02 GMT+0000 (GMT)
This is definitely not what I expected or want.
All I want is to run the Jobs every 45 minutes.
Can anyone help me with the pattern?
Thanks :)
You're probably looking for
0 */45 * * * *
The ranges are here.
Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11
Day of Week: 0-6
I'm more familiar with cron than with node-cron, but I've taken a quick look at the documentation.
If I understand it correctly, node-cron uses a syntax similar to that used by cron, but with an additional "seconds" field. So where a cron job might have:
# min hour mday month wday command
*/15 * * * * some-command
to schedule some-command to run every 15 minutes, node-cron would use a similar syntax to specify the time to run:
'0 */15 * * * *'
(with an additional field to specify seconds), but it executes a specified JavaScript function, not an external command.
In standard cron, there is no syntax to specify running a job every 45 minutes. A specification of 0/45 * * * * would run a job twice each hour, at 0 and 45 minutes after the hour. To run a job every 45 minutes (at 00:00, 00:45, 01:30, 02:15, ..., i.e., 32 times per day) you'd have to schedule it to run every 15 minutes, and then invoke a script that checks the current time to decide whether to do anything.
Or you can write an exhaustive list of all the times you want the job to run:
0 0 * * * some-command
45 0 * * * some_command
30 1 * * * some_command
15 2 * * * some_command
# 28 lines omitted
I'd definitely want to write a script to generate this list.
(This is workable because 24 hours happens to be a multiple of 45 minutes.
You couldn't run something every 35 minutes this way.)
A similar approach should work for node-cron. Schedule the function to run every 15 minutes, and invoke a function that checks the current time to decide whether to run. For example, you can check whether the number of minutes since midnight modulo 45 is zero. (You might want to allow for a small variance in case the scheduling is not exact.)
I don't know JavaScript well enough to suggest the best way to write this function, but it should be reasonably straightforward.
Or write 32 lines to specify all the times you want it to run.
There is no direct way to do this. However, we can get the result by intercepting the schedule using a shell command within the target script.
First, run the script at every 15 minutes:
*/15 * * * * <target_script>
Then, within the target_script, place the following code before actual codes:
#!/bin/sh
# Exit except at 0:45, 1:30, 2:15, 3:00, 3:45 etc
if ! echo "((`date +%-H`*60)+`date +%-M`)/45" | bc -l | grep "\.0*$" &> /dev/null;
then exit 1;
fi
# Your actual code goes here;
I tried this string for a 45-second interval and it works well:
'*/45 * * * * *'
You need to write a script as a wrapper to decide if the actual command shall be executed at every 45 minutes. That's 0, 45, 30 (= 45 + 45 - 60), 15 (= 30 + 45 - 60), 0 (= 15 + 45 - 60). so, the minutes to run the script shall be 0,15,30,45.
The command date +%M may be helpful in the shell script.
you can use node-reel which is more readable, straight forward and awesome 😉.
const reel = require('node-reel')
reel().call(() => {
console.log(Date.now());
}).everyFortyFiveMinutes().run()
https://github.com/shakee93/node-reel
Answer: */45 * * * *
This will run every 45th minute
Have a look here for clear understanding 👇
https://crontab.guru/#/45_*_
use cron npm moduel something like this
var cronJob = require('cron').CronJob;
var job = new cronJob({
cronTime:'0 */45 * * * *',
onTick: function(){
var my_date = new Date();
var tomorrow_date = my_date.getFullYear() + "-" + ('0'+(my_date.getMonth()+1)) + "-" + (my_date.getDate()+1)
var condition = [{},{$set: {'plannedDeliveryDate' :tomorrow_date +'T00:00:00.000Z'}}]
dbQuery.updateMany(orderModel, condition, function(err, result){
if(result.nModified == result.n) console.log(err, result)
})
},
start:true,
timeZone:'Asia/Kolkata'
});
job.start();
You can refer to cronr, it supports all the macro pattern and provides online demo cronr -- online demo
Just recently (27|01|22) used Node Cron in my project
const TaskSchedulerTimeInterval = 45;
// Runs a job for every 45 mins asynchronously
const fetchYoutubeAPIScheduler = () => {
try {
nodeCron.schedule(`*/${TaskSchedulerTimeInterval} * * * *`, async () => {
console.log('Running');
})
} catch (error) {
console.log('error =', error);
}
};
Underlying Meaning (loved the explanations given by others, so referencing them together in one post) :
#sec min hour monthday month weekday
*/15 * * * * *
Above example runs every 15 secs
range of each field:
Sec: 0-59
Min: 0-59
Hour: 0-23
monthday(Day of Month): 1-31
Months: 0-11
weekDay(Day of Week): 0-6

Run Cron at Minute 0 of Specific Hour Set

What I'm trying to do is run a Cron job at specific hours such as 1,9,13,16 but only once for each of those hours. Setting it up every few hours doesn't work for me because it needs to be at specific hours.
This is what I'm currently using but it doesn't run: 0 1,9,13,16 * * *
In order to get it to run I have to use this: 0 * * * * or * * * * *
Any ideas?
0 1,9,13,16 * * * is a perfectly valid cron expression (I've just checked it with jailshell, although I was confident). It seems to me you have a problem somewhere else. Try settings the cron job using crontab -e and make a quick test with * * * * * wget google.com to see if it works at all.
Also here is a online cron job expression validator if you need it: http://www.unitedmindset.com/jonbcampos/2009/07/29/custom-validators-cron-job-expression-validator/
1 1,9,13,16 * * *
Try minute 1 instead of 0. It works for me.
But this 0 14 * * 1-5 works for me too, or this 0 1,13 * * *.

Resources