Setting cron job end time - cron

I want to set up a cron job which will execute a command every hour. However, I want that this command should be started at 10 A.M and should run every hour till 4 P.M. This job is to run daily between these times. The command is nothing but a call to a Perl script. Following crontab entry runs fine and invokes the script every hour
* */1 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt
Is there a way to limit the timings of this cron job so that it runs only between 10 A.M and 4 P.M ?

man 5 crontab is your friend. (Your example does not do what you claim it does; /1 is the default skip and therefore redundant, and that spec therefore runs once per minute due to the leading * instead of 0.)
0 10-15 * * * your command here
(I used 15, because it occurs to me that "between 10 and 4" is an exclusive range so you don't want to run at 16:00.)

If you want the script to be run every hour you can do something like this:
[code]
00 10,11,12,13,14,15,16 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt
[/code]
This means when the minutes hit 00 and the hour hits any of 10 11 12 13 14 15 16 the script will be run

In your Perl script (or in a wrapper for the Perl script), you can use localtime to check the hour and exit if it isn't between 10am and 4pm:
use strict;
use warnings;
my #lt=localtime;
my $hour=$lt[2];
unless($hour>=10 and $hour<=16)
{
print "Not between 10am and 4pm. Exiting.\n";
exit;
}
#put the rest of your code here.

Related

How to set crontab every 1 hour 1 minute

I want to schedule a command every 1 hour and 1 minute. For example, if the first command executes at 01:01 pm, the next command will execute at 01:02PM; the time between the command executions is 1 hour and 1 minute.
I tried using
*/1 */1 * * *
but it runs every minute. Can anyone help me?
There's no way in crontab to schedule a job to run every 61 minutes (which, BTW, is an odd thing to want to do), but you can do it indirectly.
You can schedule a job to run every minute:
* * * * * wrapper_script
where wrapper_script invokes the desired command only if the current minute is a multiple of 61, something like this:
#!/bin/bash
second=$(date +%s)
minute=$((second / 60))
remainder=$((minute % 61))
if [[ $remainder == 0 ]] ; then
your_command
fi
This sets $minute to the number of minutes since the Unix epoch, 1970-01-01 00:00:00 UTC. You can adjust when the command runs by using a value other than 0 in the comparison.
That's assuming you want it to run every 61 minutes (which is what you asked). But if you want to repeat in a daily cycle, so it runs at 00:00, 01:01, ..., 23:23, and then again at 00:00 the next day, you can do it directly in crontab:
0 0 * * * your_command
1 1 * * * your_command
2 2 * * * your_command
# ...
21 21 * * * your_command
22 22 * * * your_command
23 23 * * * your_command
You can use this method which tells it to run every 61 minutes after the cron job.
while true
do
# do stuff here every 61 minutes
sleep 61m
done
Another option:
Cron can easily run every hour, but 61 minutes is harder to achieve.
The normal methods include using a sleep command or various rather
elaborate methods in the script itself to fire off every 61 minutes.
A much simpler method is using cron's cousin, the at command. The at
command will run through a file and run all the commands inside, so
you just need to place the commands in a file, one per line, then add
this line to the bottom of the file:
at now + 61 minutes < file
The commands can be any type of one-liner you want to use.
Here is an example. Call this file foo and to kick off the execution
the first time, you can simply run: sh foo
date >> ~/foo_out
cd ~/tmp && rm *
at now + 61 minutes < ~/foo
That will output the date and time to ~/foo_out then move to a tmp
directory and clean out files, then tell the at command to run itself
again in 61 minutes which will again run the at command after
executing the rest.

Using crontab to execute script between 17:00–20:00 for every 10 minutes

I want to send emails to clients every day between 17:00 to 20:00. I want to run my command every 10 minutes in this period.
So the script will be executed 6 times per hour. That's a total of 18 times.
Is this possible with the crontab? How should I write the syntax?
I think this should work:
0/10 17-19 * * * <cmd>
or:
0/10 17,18,19 * * * <cmd>

Running a cron job randomly for every one hour

I want a cronjob to run every one hour randomly. (i.e if the first job runs at 58 minutes,the second job should run at 47 minutes and the third one at 52 minutes and so on) But this should run randomly for everyone hour. Is there a way to do this?
You could run a job every hour, on the hour, that sleeps up to 3,599 seconds and then executes your script:
0 * * * * /path/to/perl -e 'sleep int rand 3600' && /path/to/yourScript
Or, using PHP if you prefer that to Perl:
0 * * * * /path/to/php -r 'sleep(rand(0,3599));' && /path/to/yourScript
You can find the path to Perl with:
which perl
likewise for PHP:
which php
Instead of using perl or even php, just use the BASH $RANDOM built in divided by 3600 which equals one hour like so.
0 * * * * sleep $((RANDOM%3600)) && /path/to/yourScript
Keep in mind that you will probably have some race conditions with a script sleeps randomly close to an hour depending on how long it takes for your script to execute.

Is this the correct way to run a script at certain hours?

I'd like a cron job to run a command at:
8am
10am
5pm
7pm
9pm
11pm
I've tried this, but when I check my logs, I see that the command is run too many times.
* 8,10,17,19,21,23 * * * COMMAND >> mylog.txt
Your command means "run on every minute of the given hours". If you want to run it just once, you need to set the minute field, e.g.:
0 8,10,17,19,21,23 * * * COMMAND >> mylog.txt
This way the command only runs at the start (minute 0) of each of the given hours

Cron runs after other cron

I want to set a cron run after an other cron. For example: Cron A finishs at 01:00 PM, cron B will start at 01:01 PM. The problem is I don't know when cron A finishs.
I checked the crontab syntax. It doesn't provide any param for that purpose.
My actual situation is:
# This cron must run first.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
# two these crons runs at the same time.
0 13 * * * /usr/local/bin/php -f /path/update_user.php
0 13 * * * /usr/local/bin/php -f /path/update_image.php
# This cron runs right after two above cron completes.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
You can use the batch command inside the first cron to have the second thing being scheduled to run.
Your first job could produce a timestamp when finished.
Then you estimate - for example - that job A needs about 60 to 90 minutes. After 60 minutes, you start job B. Job b looks for the timestamp. If it is present, job B starts, else it waits for a minute and looks again.
After finishing, job B deletes the timestamp, or renames it, maybe from 'todo' to 'done'. You could insert the current date inside the file, to check, whether your estimation is still acceptable, or should be adjusted.
What I do in such cases (commonly a backup scenario where I don't want to thrash the disk by having concurrent backups) is to write a script that cron calls, and in the script have the actual tasks run serially.
Something like:
#!/bin/bash
/usr/local/bin/php -f /path/update_user.php
/usr/local/bin/someOtherTaskToRunSecond
YMMV.

Resources