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

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

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'

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

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

How to reboot via cron on scheduled basis. Ubuntu 14.04

I have a very simple script that works from the command line.
#!/bin/bash
reboot
When I put a call to execute the script into root users crontab -e using the following format it does not run. It does run the first two commands, just that last one is giving me grief. I have no MTA installed as I do not need it.
*/10 * * * * service jwtpay restart
0 3 * * * bash /root/backup/mongo.backup.s3.sh kickass /root/backup >/dev/null 2>&1
0 */3 * * * bash /root/reboot.sh >/dev/null 2>&1
What am I missing?
Maybe the script is not executable... Since you use root's crontab why call the binary via a script and not the binary itself? Use the full path to the binary. It may vary on your system. Find out where it is with which reboot.
0 */3 * * * /sbin/reboot
Don't forget to restart the cron daemon, after changeing the crontab.

How to check if appened cron job has no errors while executing?

I have added a three different executable scripts - bash, php, python scripts on to a crontab as cronjob
How would I check that three executable scripts are not giving any errors or not having any errors while executing from cron tab ?
Thanks !!
Not sure I understand the question, is this 3 different cron entries, or is three scripts sharing the same cron entry? For the latter (one cron entry for three scripts), do this (for a cron run every minute):
* * * * * /bin/bash script.sh && /usr/bin/php script.php && /usr/bin/python script.py
The php will only run if the bash succeeds. Likewise, the python will only run if both the bash and php script succeed. If you need all scripts to run regardless if one of them fails, have each job run from their own cron entry. In which case, do this:
* * * * * /bin/bash script.sh 2>>/var/log/script.sh.errors && echo "Bash Completed Successfully" >>/var/log/script.sh.success
* * * * * /usr/bin/php script.php 2>>/var/log/script.php.errors && echo "PHP Completed successfully" >>/var/log/script.php.success
* * * * * /usr/bin/python script.py 2>>/var/log/script.py.errors && echo "Python Completed successfully" >>/var/log/script.py.success
In the second example, errors log to a separate file. Scripts can return a exit code 0 (i.e. "Success") even if there are errors (for instance, if the script actually has an exit 0 statement at the end of it). Thus, it's best to try to capture errors as the script runs, then capture a successful exit code, if it exits successfully.

Resources