Are there cron-expressions to run a script every ninth minute? - cron

I'd like to run a bash command every 9th minute and every 70th minute.
man -S 5 crontab says:
Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".
I ran the command
echo "Cron-job runs. ($(date))" >> crontest.log
with the cron expression
*/9 * * * *
which gave me
Cron-job runs. (We 25. Mar 13:27:01 CET 2015)
Cron-job runs. (We 25. Mar 13:36:01 CET 2015)
Cron-job runs. (We 25. Mar 13:45:01 CET 2015)
Cron-job runs. (We 25. Mar 13:54:01 CET 2015)
Cron-job runs. (We 25. Mar 14:00:01 CET 2015)
Cron-job runs. (We 25. Mar 14:09:01 CET 2015)
Cron-job runs. (We 25. Mar 14:18:01 CET 2015)
It seems, that */9 means "every minute that can be divided by nine without remainder" (this includes zero!) instead of "every ninth minute".
Are there cron-expressions to define intervals?

No, there is no facility for this specific use case. You could create a complex crontab which lists all the combinations;
0-54/9 0-21/3 * * * crontest
3-57/9 1-22/3 * * * crontest
6-51/9 2-23/3 * * * crontest
(This complex syntax is an extension, but since you were asking about */9 which is a similar extension, you should be able to use the above as well. See an earlier version of this answer for the full-hand syntax, which however I incorrectly identified as extended in my original answer.)
For execution every 70 minutes, a similar table would be a lot more complex, because 70 is not evenly divisible by 1440 (24*60). You end up with a periodicity over multiple days, so the table gets so complex as to beg for alternative solutions.
... One of which would be to use a self-scheduling at job:
#!/bin/sh
echo "$0" "$#" | at now + 70 minutes
:
# the rest of your crontest script here
You need to start this one manually the first time, but it will keep scheduling new jobs after that. (If your server is down for an extended period of time, you may need to restart it; but the job should survive the occasional quick reboot just fine.)

Or you could run your script every minute, and the script can check if it's the right time to run.
untested
#!/bin/bash
minute_of_the_epoch=$(( $(date +%s) / 60 ))
(( minute_of_the_epoch % 9 != 0 )) && exit
# rest of script ...

Related

Calculating the time until the start of next hour in Bash

Hi I have a bash script called by a systemd service that needs to run until the start of the next hour. Currently I have been using:
currentTime=$(date +"%s")
nextHour=$(date -d "$(date -d 'next hour' '+%H:00:00')" '+%s')
duration=$(((nextHour-currentTime)*1000))
Which works except for trying to calculate the difference between 11pm and midnight were as far as I can tell it gets the current days midnight from 23 hours previous.
Oct 13 23:00:05 host bash[2019]: 1665698405
Oct 13 23:00:05 host bash[2019]: 1665615600
Oct 13 23:00:05 host bash[2019]: -82805000
I figure I could put a conditional check with a different calculation if needed or perhaps look at a systemd timer for triggering the service but as the service needs to always activate on boot/reboot as well as running hour to hour this setup seemed more appropriate.
Would appreciate any advice on why this is happening and advice on most streamlined steps to avoid it.
This isn't really a question about bash, but seems to be more about date. Given that date has multiple different implementations, it seems wiser to choose a different tool to do the calculation. I suspect perl is more standardized and (almost) as available as date, so you might try to get the difference with:
perl -MTime::Seconds -MTime::Piece -E '
my $t = localtime; my $m = ($t + ONE_HOUR)->truncate(to => "hour"); say $m - $t'

Special crontab expression

just to verify my understanding of cron-syntax, the following expression will fire on a saturday at 02:42 in the middle of the month, right?
42 02 12-19 * 6 myScript > /dev/null 2>&1
cheers
Nicolaie
The script myScript (not good without a path here!) will be executed at 2:42 indeed, on every day between and including the 12th to the 19th of each month that is a saturday.
Actually
42 02 12-19 * 6 myScript > /dev/null 2>&1
means run on 12 thru 19 and every Saturday.
You need a more complex line to do what you state:
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"
is an example from the manfile on my system. It uses a trivial execution after making sure it is Saturday.
See http://www.adminschoice.com/crontab-quick-reference/ as well as > man 5 crontab (at the commandline) for more.

Run a script at specified time in linux

I have created scripts and now I want them to run automatically at specified time, how can I do that?
I am using Ubuntu and JAVA to write my scripts.
You can use crontab to do this in a specific time continuously.
To edit a crontab entries, Login as root user (su – root) and do crontab -e as shown below. By default this will edit the current logged-in users crontab.
root#dev-db# crontab -e
Scheduling a Job For a Specific Time
The basic usage of cron is to execute a job in a specific time as
shown below. This will execute the Full backup shell script
(full-backup) on 10th June 08:30 AM.
Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.
30 08 10 06 * /home/ramesh/full-backup
More example here, and the crontab utils can be found here

Create Cron job to execute every wed at 2nd week

What the correct way to execute cron job to be run at
Wed in 2nd / week and wed in 4th week of each month
any tips
Following would run the job on every wednesdays at 11 AM
00 11 * * 3 <your-command>
Here the 3 says, the command to run every Wednesday. You would need to create a wrapper script, to just skip the execution on alternate Wednesday, and call that script from above cron.

Setting cron job end time

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.

Resources