Cron Jobs Run At Same Time - cron

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

Related

How to schedule multiple CronJobs using bash without conflicting each other?

I have 4 Jobs, which run at different intervals. How can I prevent them from conflicting each other? Job 2,3,4 can only be run one at a time. Any new job invocation must wait for old completion before beginning.
0 9,11,14 * * 1-5 /bin/bash /home/userName/Desktop/Auto/job_1.sh
0 8-17 * * 1-5 /bin/bash /home/userName/Desktop/Auto/job_2.sh
*/6 * * * * /bin/bash /home/userName/Desktop/Auto/job_3.sh
*/20 * * * * /bin/bash /home/userName/Desktop/Auto/job_4.sh
Any help is much appreciated. Thanks!
I would look into using flock.
You have to install util-linux to get flock.
It has lots of options like timeout, etc.
Your crontab could look something like this:
0 9,11,14 * * 1-5 flock -x /tmp/cronjobs.lock -c '/bin/bash /home/userName/Desktop/Auto/job_1.sh'
0 8-17 * * 1-5 flock -x /tmp/cronjobs.lock -c '/bin/bash /home/userName/Desktop/Auto/job_2.sh'
*/6 * * * * flock -x /tmp/cronjobs.lock -c '/bin/bash /home/userName/Desktop/Auto/job_3.sh'
*/20 * * * * flock -x /tmp/cronjobs.lock -c '/bin/bash /home/userName/Desktop/Auto/job_4.sh'
The syntax for flock is:
flock -x <lockfile> -c '<command>'
The lockfile is a file that is locked on your machine. Each new command will check to see if that file is locked by a previous command. Once that previous command finishes, it releases the lock and the next command can run, taking out a new lock.
Using the -w <seconds> command you can tell flock the time in seconds to wait while trying to take out a lock on the file before the command fails and does not run.
For instance, the following would wait 3 minutes for previous cron job to finish. If it did not finish in that time then the command below would not run.
*/20 * * * * flock -w 180 -x /tmp/cronjobs.lock -c '/bin/bash /home/userName/Desktop/Auto/job_4.sh'

Cronjob won't trigger .sh script

I have made this Cronjob that should run some tests. My script works, but cronjob won't trigger it.
Cronjob looks like this:
*/1 * * * * /bin/sh cd ~/Desktop/abc.sh
I want it to run every minute, just for testing purposes.
And my script is:
while read LINE; do curl -o /dev/null --silent --head --write-out "%{http_code} $LINE\n" "$LINE"; done < todo | tee test_results.txt
I can't even find the solution on google or youtube.
If you added #!/bin/bash to your script, then your cronjob should look like:
* * * * * ~/Desktop/abc.sh
Or
* * * * * /home/USER/Desktop/abc.sh
In the first case you have to run cronjob from the same user where of the Desktop folder.

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 would I get a cron job to run per 30 minutes?

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

Ping website url from cronjob apache

I need to setup simple cronejob to ping my website url every 2 minutes, what i did for now make no effect, can somebody help me write simple ping cronjob?
Here is what i have for now
2 * * * * /usr/bin/wget -O - -q -t 1 http://example.com/api/get.php?refresh=yes
This might help:
*/2 * * * * wget -q -O - "http://example.com/api/get.php?refresh=yes" >/dev/null 2>&1
Every 2 minutes (15 in the article), ping the specified file without
any output whatsoever. Just a silent ping to execute my script,
nothing more, nothing less.
Source: https://wp-mix.com/cron-notes-ping-external-url-with-no-output/

Resources