How would I get a cron job to run per 30 minutes? - cron

I put below into crontab,but it not working.
*/30 7-20 * * * pgrep -f crawl_index.py > /dev/null || python3.6 /htdocs/crawl/crawl_index.py >> /var/log/py-crawl.log 2>&1
*/10 7-20 * * * pgrep -f download_url.py > /dev/null || python3.6 /htdocs/crawl/download_url.py >> /var/log/py-download.log 2>&1
but when I run pgrep -f download_url.py > /dev/null || python3.6 /htdocs/crawl/download_url.py >> /var/log/py-download.log 2>&1 it works

Most of the cron executables will execute a script every 30 minutes with:
*/30 * * * * (command to be executed)
For older cron executables that don't understand the */x notation, the script can be run every 30 minutes by adding the below line in your crontab:
0,30 * * * * (command to be executed)
This command runs at the 0th and 30th minute of every hour, so basically every 30 minutes. The minutes 0 and 30 may be changed based on your requirement to 1 and 31 or 2 and 32 etc, still running every 30 minutes.
See this link for the documentation

Related

Crontab every 5 minutes, but not on 5,10,15, etc

Some of my sites need regular crontabs, I use this to start a cronjob every 5 minutes "*/5 * * * *".
The crontabs are small, light, but there are starting to be several sites that need them, and starting them all together, it starts not being a very good idea.
With this "*/5" the cron starts at 5, 10, 15 20, etc... is it possible to make it start at, for example 8,13,18,23, etc?
Vixie cron accepts steps in a range (thanks Keith Thompson), so you can do
3-58/5 * * * * my_command
With other versions of cron, this may not be supported and you'd just have to do
3,8,13,18,23,28,33,38,43,48,53,58 * * * * my_command
Another option is something like
*/5 * * * * sleep 3m ; my_command
This could be adapted to sleep for a random time, thus further spreading out the jobs. For instance, you could do
*/5 * * * * /bin/bash -c 'sleep $((RANDOM/(32767/180))) ; my_command'
or use SHELL = /bin/bash further up in your crontab to make the /bin/bash -c unnecessary, if you're okay with using bash to run all the other cron jobs following the SHELL = line. $RANDOM in bash expands to a random integer between 0 and 32767, so this gets you a delay of up to 180 seconds.

Cron jobs not running in VestaCP (CentOs)

I have written some PHP scripts that I am trying to run with cron jobs in VestaCP, but they don't seem to be running. I have tried to search for other threads on here and on the VestaCP forum that could help me identify the error, but have not found a solution.
Server system:
CentOs 7.4
Vesta 0.9.8-22
I have tested the PHP scripts by going to the links directly. They all work well. But the cron jobs are not running and I can't figure out why.
I have not been able to locate any error messages or logs generated by these cron jobs (even when I remove the "> /dev/null 2>&1"). But I might have been looking in the wrong places.
All of the cron jobs have been added through the VestaCP cron interface.
I have disabled exim, dovecot, clamd, and spamassassin. And I have turned off notifications in the cron panel. Not sure if that is related.
Copied from /var/spool/cron/admin [edited domain name]:
15 02 * * * sudo /usr/local/vesta/bin/v-update-sys-queue disk
10 00 * * * sudo /usr/local/vesta/bin/v-update-sys-queue traffic
30 03 * * * sudo /usr/local/vesta/bin/v-update-sys-queue webstats
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-queue backup
10 05 * * * sudo /usr/local/vesta/bin/v-backup-users
20 00 * * * sudo /usr/local/vesta/bin/v-update-user-stats
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-rrd
15 6 * * * sudo /usr/local/vesta/bin/v-update-sys-vesta-all
01 4 * * * sudo /usr/local/vesta/bin/v-update-letsencrypt-ssl
*/5 * * * * wget -q -O - "https://testing.example.com/cli/new-projects" > /dev/null 2>&1
*/5 * * * * wget -q -O - "https://example.com/cli/new-projects" > /dev/null 2>&1
30 10 * * * wget -q -O - "https://example.com/cli/project-expiration" > /dev/null 2>&1
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-queue letsencrypt
0 10 * * * wget -q -O - "https://testing.example.com/cli/project-expiration" > /dev/null 2>&1
*/2 * * * * wget -q -O - "https://testing.example.com/cli/email-sender" > /dev/null 2>&1
*/2 * * * * wget -q -O - "https://example.com/cli/email-sender" > /dev/null 2>&1
I had the same problem in Ubuntu.
The problem was that the cron jobs created from VestaCP control panel is created for the user and although there is sudo at the beginning of the command, they are not run.
They seem to be called since I can see them on the /tmp/log/syslog file. It does not show any error though. But for some reason, the commands are not executed.
Here are few simple commands to check cron status.
Check if Cron service is running:
pgrep cron
if a number is returned the service is running else not
Check Cron status:
systemctl status cron
Check the current cron file:
crontab -l
Edit cronjob file:
crontab -e
One solution is to create the cron jobs for the root user from the terminal. I have not tried for other users. The cron jobs created for root user will run without a problem.

How to set crontab every 1 hour 1 minute

I want to schedule a command every 1 hour and 1 minute. For example, if the first command executes at 01:01 pm, the next command will execute at 01:02PM; the time between the command executions is 1 hour and 1 minute.
I tried using
*/1 */1 * * *
but it runs every minute. Can anyone help me?
There's no way in crontab to schedule a job to run every 61 minutes (which, BTW, is an odd thing to want to do), but you can do it indirectly.
You can schedule a job to run every minute:
* * * * * wrapper_script
where wrapper_script invokes the desired command only if the current minute is a multiple of 61, something like this:
#!/bin/bash
second=$(date +%s)
minute=$((second / 60))
remainder=$((minute % 61))
if [[ $remainder == 0 ]] ; then
your_command
fi
This sets $minute to the number of minutes since the Unix epoch, 1970-01-01 00:00:00 UTC. You can adjust when the command runs by using a value other than 0 in the comparison.
That's assuming you want it to run every 61 minutes (which is what you asked). But if you want to repeat in a daily cycle, so it runs at 00:00, 01:01, ..., 23:23, and then again at 00:00 the next day, you can do it directly in crontab:
0 0 * * * your_command
1 1 * * * your_command
2 2 * * * your_command
# ...
21 21 * * * your_command
22 22 * * * your_command
23 23 * * * your_command
You can use this method which tells it to run every 61 minutes after the cron job.
while true
do
# do stuff here every 61 minutes
sleep 61m
done
Another option:
Cron can easily run every hour, but 61 minutes is harder to achieve.
The normal methods include using a sleep command or various rather
elaborate methods in the script itself to fire off every 61 minutes.
A much simpler method is using cron's cousin, the at command. The at
command will run through a file and run all the commands inside, so
you just need to place the commands in a file, one per line, then add
this line to the bottom of the file:
at now + 61 minutes < file
The commands can be any type of one-liner you want to use.
Here is an example. Call this file foo and to kick off the execution
the first time, you can simply run: sh foo
date >> ~/foo_out
cd ~/tmp && rm *
at now + 61 minutes < ~/foo
That will output the date and time to ~/foo_out then move to a tmp
directory and clean out files, then tell the at command to run itself
again in 61 minutes which will again run the at command after
executing the rest.

Cron Jobs Run At Same Time

In my cron job file I have two cronjobs defined:
#Yo1 MAILTO="example#domain.com"
*1****wget -O - -q "http://example.com/cron/test1.php">/dev/null 2>&1
#Yo1 MAILTO="example#domain.com"
*15****wget -O - -q "http://example.com/cron/test2.php">/dev/null 2>&1
The PHP files are simple just sending mails with different subjects.
The issue is that both cronjobs are running on the same time every minute, but as you can see I want them to run on different times. First - every minute, second - every 15 minutes.
Can you help me with this. I can't figure out whats wrong.
Your syntax is incorrect.
Please use the following code
#every minute
* * * * * wget -O - -q "http://example.com/cron/test1.php">/dev/null 2>&1
#every 15 minutes
*/15 * * * * wget -O - -q "http://example.com/cron/test2.php">/dev/null 2>&1
You can use online crontab generators like http://www.crontab-generator.org/
* * * * * wget -O - -q "http://example.com/cron/test1.php">/dev/null 2>&1
15 * * * * wget -O - -q "http://example.com/cron/test2.php">/dev/null 2>&1

CentOS Unix Cron order of execution when all set to same time

I have a series of cron jobs running at the command line calling the php interpreter all by the same user configured to run once a day
0 0 * * * /usr/bin/php -q /mydirectory/myphp.php
0 0 * * * /usr/bin/php -q /mydirectory/myphp2.php
0 0 * * * /usr/bin/php -q /mydirectory/myphp3.php
Do these all execute at once or do the execute in the order of entry in some cron table, complete and move on to the next cron job?
And to answer your question despite the off-topic:
They will get executed in parallel, not sequentially. If you need some order it would pay to add them all to one script, and execute them sequentially separated by &&, e.g.
#!/bin/bash
/usr/bin/php -q /mydirectory/myphp.php && /usr/bin/php -q /mydirectory/myphp2.php && /usr/bin/php -q /mydirectory/myphp3.php

Resources