special cronjob scheduling - linux

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.

Related

Crontab how to disable emails

I want to disable email reports on some tasks which run frequently. I've gone through the following links
https://unix.stackexchange.com/questions/84335/stop-cron-sending-mail-for-backup-script
https://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/
They suggest adding >/dev/null 2>&1 at the end of the command to disable emails.
This is my crontab entry :
* * * * * /bin/bash /home/ubuntu/startup/monitor-mosquitto.sh >/dev/null 2>&1
But I'm still receiving emails every time the script is run. In fact, not just once but I get like 8-15 mails every time it runs.
Am I doing anything wrong here ? BTW, I'm using crontab as root ( sudo crontab -e )
See man 5 crontab:
If MAILTO is defined but empty (MAILTO=""), no mail will be sent.
If memory serves, I have used that on the line itself, or preceding it:
MAILTO=""
* * * * * /bin/bash /home/ubuntu/startup/monitor-mosquitto.sh
Note that this will affect all lines that follow it so you may want to place it last, or renable MAILTO.
Also, strictly speaking, you should be able to work out what you did with shell redirection in the shell itself. What you have looks correct so I am a little puzzled. Maybe make sure to test it as root not as you.

Cron periodically for different scripts

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).

Running a crontab every 15 minutes not working on linux redhat

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.

Cron Jobs not running in Linux Mint 12

This is the content of my crontab -e file
#!/bin/bash
6 14 * * * /home/rishi/cront.sh
Also, the cront.sh file has only this
mkdir foo
I have been trying to make this work since the last 2 days. The cront.sh command works when ran from the terminal. But, does not work from crontab.
EDIT
It turned out that just editing the crontab -e using root
did the job. Nothing more had to be done.
Under cron, there is no guarantee that your environment variables (most importantly PATH) will be set proprerly.
Try adding line like this at the top of your crontab:
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
Also, it would be good idea to use full path for mkdir:
mkdir /path/to/my/dir
Also, it would not hurt to make sure that your cront.sh is executable:
chmod +x /home/rishi/cront.sh
After that, it should work.
EDIT
Generic method to debug crontab issues:
At the top of your script to debug, add a line:
set # this should print all environment variables
Execute your script manually, redirect output to some log file1.
Now, edit crontab to be something like this:
* * * * * /path/to/my/script 2>&1 > /path/to/log/file2
Be sure that log file will be writable for your script.
Also, be sure that your script has executable bit set.
Compare log file1 and log file2, paying close attention to env. variables. If they differ, use whatever method you want to set them to be the same. It could be adding lines to crontab, or using export var=value in your scripts.
After that, there is no reason for this to not work properly.
You don't need to first line
#!/bin/bash <--- remove this line
6 14 * * * /home/rishi/cront.sh
Is your script executable?
If it is not. Try running the following command in a terminal.
chmod +x /home/rishi/cront.sh
An example of crontab format with commented fields is as follows:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /your/path/yourscript.sh
try
6 14 * * * sh /home/rishi/cront.sh
or
add first line in cront.sh
#!/bin/bash
or chmod a+x /home/rishi/cront.sh

Cron command to run PHP script every 10 minute

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>

Resources