I have a server running Linux operating system. I am trying to schedule a cron job to run a task every two weeks (Fortnight) in Tuesday at 9am.
I can only manage to run the task manually by comment (if I don’t want to run the job for this week) and uncomment (if I want to run the job for this week) as following:
0 9 * * 2 root java -jar test.jar // will run
# 0 9 * * 2 root java -jar test.jar // will not run
I have attempted to use the following cron job:
0 9 * * 2 case $(($(date +\%s) / (60*60*24*7))) in *[02468]) root java -jar test.jar
But this cron script does not seem to work.
Any thought
I would try to execute at 4AM every one of two tuesdays :
0 4 * * 2 test $((10#$(date +\%W)\%2)) -eq 1 && execute_cmd
We first get week number with date and correct formatting, then the 'one time out of two' thing is done by using 'test' command.
'test' evaluates to true or false depending on a given expression, here, it evaluates whether $((10#$(date +\%W)\%2)) equals 1 (hence -eq 1), in other words it returns true if the week number is odd.
So this crontab will execute on odd weeks, on tuesday (2) at 4 AM (0 4).
More in-depth details here :
https://serverfault.com/questions/633264/cronjob-run-every-two-weeks-on-saturday-starting-on-this-saturday
Related
I have a server running Linux operating system. I am trying to schedule a cron job, in crontab file, to run a task every two weeks (Fortnight) on Tuesday at 9 am. I tried to run the following command:
0 9 * * 2 root test $((10#$(date +\%V)\%2)) -eq 0 && ( java -jar /email/emailRemind.jar )
This script does not work, it shows this message
/bin/sh: 1: arithmetic expression: expecting EOF: 10#24%2"
Any thoughts?
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.
I need some help in writing a Linux script that do the following:
command 1
command 2
wait 10 minutes
command 3
command 4
and this script should run automatically at specific time for example 4 am...
Thank in advance
You can create a script.sh like:
#!/bin/bash
command 1
command 2
sleep 600 # 600 seconds = 10 min
command 3
command 4
And then create a cronjob:
0 4 * * * /bin/bash /path/to/script.sh
You can see more info of cron in https://stackoverflow.com/tags/cron/info
if you want the job to run once at a future time, instead of cron use at
at 4am tomorrow <<END
command 1
command 2
sleep 600
command 3
command 4
END
One of the advantages of at is that it will execute the commands using your current environment. The limited environment provided by cron is a cause of confusion for many people.
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.
How can i set three cron jobs for every two days interval but not runs all.
This is is my three cron job process:
0 20 */2 * * /usr/local/bin/ruby /var/www/html/data_download1.rb >/dev/null 2>&1
0 20 */2 * * /usr/local/bin/ruby /var/www/html/data_download2.rb >/dev/null 2>&1
0 20 */2 * * /usr/local/bin/ruby /var/www/html/data_download3.rb >/dev/null 2>&1
You can use 1-31/2 for one task and 2-30/2 for the other to specify alternating days. This will fail on months with odd numbers of days as the 1-31/2 job will run twice in a row.
If you absolutely must have it right, you can check whether the number of days since a particular date is odd or even and have one script abort if it's odd and the other abort if it's even.