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'
I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.
I run several cron jobs every minute - I use flock to prevent overlapping as a couple of the scripts may run for over minute:
* * * * * flock -n /path/to/lock-process-1.txt php /path/to/process-1.php
* * * * * flock -n /path/to/lock-process-2.txt php /path/to/process-2.php
However, all the processes run at the same time (and most of the processes only take a few seconds). So I'd like to stagger the crons so that they are ten seconds apart. I have read elsewhere that this can be done with sleep (from this post):
* * * * * sleep 10;curl http://www.google.com/
My question is: can I use sleep alongside flock and if so, where do I add the sleep 10;
I went ahead and tested this and it is OK to have sleep before flock:
* * * * * sleep 10; flock -n /path/to/lock-process-2.txt php /path/to/process-2.php
Simple question: I want to run a cron operation every minute at the 10th second (for example at 2:00:10 PM). The cron operation runs a curl command to download a website page. How can I run a cron operation to do that?
Current crontab setting:
* * * * * curl http://www.google.com/
* * * * * sleep 10;curl http://www.google.com/
I want to create cron job that runs a script every 5 seconds. Seeing that cron jobs only allows increments of minutes 0-59 and so on.
I thought to create another script that calls my original script written below.
#!/bin/bash
while true
do
# script in the same directory as this script. is this correct?
bash makemehappy.sh
sleep 1
done
I now, need to know how to run this script every time i boot my computer and for it to start itself if it isn't running for some reason.
I am also aware that running this script every minute wouldn't be a good thing. :)
if there is an easier way to run a script every 5 seconds please advise.
Please and thank you.
I wouldn't use cron for this. I would use that bash script (use an absolute path, unless you want it to be portable and know that the directory structure will be preserved).
Instead, I would just sleep 5, just like you did (only 5 seconds instead of 1).
As far as starting it with your system, that depends on the system. On (some) Linux distros, there's a file called /etc/rc.local in which you can add scripts to run when the system starts. Well... I shouldn't be so general, the distros that I have used have this. If you're running Ubuntu, there is no longer an inittab, they use upstart, btw.
So if you have an endless loop and an entry in /etc/rc.local, then you should be golden for it to run endlessly (or until it encounters a problem and exits).
Try using anacron or, better yet, an init script to start when the computer starts.
If you want the script to "restart itself", you'll need to run something every few minutes to check the original is still running. This can be done in inittab (/etc/inittab) or, on Ubuntu, /etc/event.d. Try man 5 inittab, looking at the 'respawn' option.
Some crons have an #reboot time specifier (this covers all the time and date fields). If yours does, you can use that. If this is a "system" service (rather than something running for yourself), the other solutions here are probably better.
Init scripts are fine at boot, but don't detect if a process fails and has to be restarted. supervisord does a great job of detecting failed processes and restarting them. I'd recommend a script with a 5-second loop like #Tim described, but wrap supervisord around it to make sure it keeps running.
As explained in detail in my answer to a similar question, you can use SystemD timer units with whatever schedule that you want - down to a theoretical 1 nanosecond schedule with no sleep kludges
Quick overview:
Setup a SystemD service to run what you want - this can be as simple as:
/home/myusuf3/.config/systemd/user/makemehappy.service
[Unit]
Description=Make me happy
[Service]
ExecStart=/home/myusuf3/.local/bin/makemehappy.sh
Setup a SystemD timer with the schedule that you want, as documented in man systemd.timer:
/home/myusuf3/.config/systemd/user/makemehappy.timer
[Unit]
Description=Schedule to make me happy
[Timer]
OnBootSec=5
OnUnitActiveSec=5
AccuracySec=1
Enable and start the timer:
:
systemctl --user daemon-reload
systemctl --user enable makemehappy.timer
systemctl --user start makemehappy.timer
(after you enable it, it will autostart every time you start the computer, but you probably want to start it now anyway).
To answer the question in the title, this is how to run a cronjob every 5 seconds :
* * * * * /path/to/script.sh
* * * * * sleep 5 && /path/to/script.sh
* * * * * sleep 10 && /path/to/script.sh
* * * * * sleep 15 && /path/to/script.sh
* * * * * sleep 20 && /path/to/script.sh
* * * * * sleep 25 && /path/to/script.sh
* * * * * sleep 30 && /path/to/script.sh
* * * * * sleep 35 && /path/to/script.sh
* * * * * sleep 40 && /path/to/script.sh
* * * * * sleep 45 && /path/to/script.sh
* * * * * sleep 50 && /path/to/script.sh
* * * * * sleep 55 && /path/to/script.sh
It's not beautiful, but this solution comes with no extra tools nor dependencies. So if you have working cron jobs, this should work right away.