I would like to run several scripts every half-hour. This obviously would work with this line
*/30 * * * * script.sh
My question now is how I would be able to run several of these at different times. As in, script.sh to run 5 minutes before script2.sh which then is 5 minutes before script3.sh. If that shouldn't be possible, any way to ensure that they aren't executed within 5 minutes of each other would suffice.
I did see solutions to do this with a script or otherwise programmatic. If cron can't be used for the job a "not possible" is what I'm looking for as the answer.
*/30 is equivalent to 0,30. If you use the latter syntax you can simply use 5,35 for another job to offset it from the first.
For example, for three jobs you could do:
0,30 * * * * script1.sh
5,35 * * * * script2.sh
10,40 * * * * script3.sh
Note that if script1.sh takes longer than 5 minutes to run, there will still be overlap between script1.sh and script2.sh. If you really must avoid that, you should probably consider lock files (using e.g. flock).
Related
I want to run a crontab every 15minutes. I tried this:
0 */15 * * * ./usr/My_PATH/run.sh
But I get this error:
0 command not found
Is there something wrong with the syntax ?
Many thanks.
UPDATE:
I corrected the script and I tried this:
*/15 * * * * /My_Path/run.sh
and this
0,15,30,45 * * * * /My_Path/run.sh
In both cases I get an error.
#1 bash: */15: No such file or directory
#2 bash: 0,15,30,45 command not found
If this:
0 */15 * * * ./usr/My_PATH/run.sh
fails with this error:
0 command not found
then you're trying to run it as a shell command. You need to feed it to the crontab command. There are several ways to do this.
crontab -l will list the current contents of your crontab; it doesn't modify it.
crontab -e will open (a copy of) your crontab in a text editor and let you modify it. This is probably the simplest way to update it.
crontab filename reads the specified file and replaces your current crontab with its contents. (If you already have a crontab, this will quietly clobber it.)
The method I recommend is to keep a separate file containing your crontab (say, crontab.txt).
First, if you already have a non-empty crontab (check with crontab -l), save it to the file:
crontab -l > crontab.txt
Make whatever additions or other changes you want to that file, and then use
crontab crontab.txt
to install the updated crontab.
You can keep backup copies (I maintain mine in a source control system) so you can recover if you mess something up. And you can do a quick crontab -e if you want to test something, then re-run crontab crontab.txt to revert to the stored crontab.
The syntax of the crontab line in your question:
0 */15 * * * ./usr/My_PATH/run.sh
is correct, but the path ./usr/My_PATH/run.sh looks like it may be incorrect. Cron jobs run from your home directory, so the path is valid only if the usr directory is directly under your home directory (and in that case the ./ is unnecessary). It's probably better to specify the full path, which can start with $HOME/.
Yes.
First field is minutes. Second field is hours. You're setting it off at zero minutes past the hour, every 15th hour. So basically - 15:00 each day.
You want:
*/15 * * * * /some_script
Furthermore - ./ - it's a relative path, and that's probably a bad idea with cron, because it doesn't chdir to run stuff. Use an absolute path to avoid confusion. If you absolutely need to be in a particular directory for the script to work, you can try:
cd /path/to/script && ./this_script
So it's quite possible that you've got broken permissions or just not finding a relative path that you're using.
I have three scripts and want every one of them to run every 3 minutes, but in way that every minute a different script is running.
for example
00:00 script1 is executed
00:01 script2 is executed
00:02 script3 is executed
00:01 script1 is executed
Is there a way to make this work via crontab in Debian?
At the moment I have it like this:
*/3 * * * * php /Scripts/script1.php &> /dev/null
*/3 * * * * php /Scripts/script2.php &> /dev/null
*/3 * * * * php /Scripts/script3.php &> /dev/null
but this would run all the scripts all 3 minutes
There might be fancier ways, but the dead simple way is just to list out the minutes you want them to run on (and the rest would of course be * for hours, days, etc):
0,3,6,9,12,15,18,21,24,27...
1,4,7,10,13,16,19,22,25,28...
2,5,8,11,14,17,20,23,26,29...
Call a wrapper script every minute.
This wrapper script looks at (minutes % 3) and calls the correct script using the remainder.
Only one line in cron: nice.
EDIT: New thoughts
You can skip the wrapper by introducing an ugly crontab line.
I would go for the wrapper (cleaner crontab, place to set and export variables,
additional control statements), but I think you should know about the possibilities.
Make the testfiles x0, x1 and x2 in /tmp, chmod +x them, with the content
echo $(date) $0 >> /tmp/x.out
Make a crontab line
* * * * * /tmp/x`echo "$(date '+\%M') \% 3" | bc`
Wait 5 minutes (maybe get coffee black for me?) and look at /tmp/x.out.
Remove the crontab entry and the new /tmp/x* files.
I maintained a database (MySQL), I would like back up some data to the database using a perl script. To save my trouble, I would like cron to do it for me, I inserted the following using crontab
*/5 * * * * blctrl /home/blctrl/code/perl/tt01.pl
However, cron never does its job, any suggestions to get it done? The Linux installed is Centos 5?
*/5 * * * * blctrl /home/blctrl/code/perl/tt01.pl
That looks like the syntax for /etc/crontab, the system-wide crontab file. The first 5 words indicate when to run the command, the 6th is the account under which to run it, and the rest of the line is the command to execute.
(The clue was that the command is under /home/blctrl, which would be the home directory for the account blctrl.)
The syntax for your own crontab, the one you feed to the crontab command, is different. You don't specify an account name, because it only runs under your own account.
Try this:
*/5 * * * * /home/blctrl/code/perl/tt01.pl
EDIT: Incidentally, the first thing I would have tried when encountering a problem like this would be to replace the command with something simple, perhaps touch /tmp/FOO. That would have told you whether the problem was with your Perl script or with your crontab.
I'm looking at a crontab on a linux server and came accross the following line
*/1 * * * * /path/to/file
To me this means run the cron every 1 minute right?
What makes it different from this?
* * * * * /path/to/file
I'm fairly new to Linux crontab so hopefully this isn't a dumb question.
'*/1' is a step definition and means every minute; it is identical to the second format.
If you would want to do something every 2 hours, you could do this:
* */2 * * * /path/to/file
See: (search for the word "Ranges")
man 5 crontab
There's no difference. The cron scheduler sees them both as 1 minute from the last run.
In my hosting i have a section for cron job like this:
(source: site-helper.com)
The PHP script is called "croned.php", which I want it to run every 10 minutes.
What I will fill in every field?
I tried but it didn't work.
Note: the full path to the script is: /home/axelzd/domains/hellodom.com/public_html/croned.php
Put */10 in the minutes whilst putting * in all other fields.
Usually you can use commas to separate the cron minutes/hours etc. - 0,10,20,30,40,50 in your minute field (but I can't guarantee your admin will take it - I know Plesk does) and * in all others . The command is more tricky, but something like this should do /usr/bin/wget -q -t 5 --delete-after URL_TO_YOUR_CRON or php PATH_TO_YOUR_PHP_FILE_ON_THE_SERVER
try this
*/10 * * * * <command_to_be_invoked>