Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to set cron job to run 45 min time interval 1

Status
Not open for further replies.

vipinhcl

Programmer
Apr 21, 2010
30
0
0
US
Hi All,

I am trying to set the job run time for .sh file from every 1 hour to 45 min time interval, but unable to set the time. I want to run the job after 45 min time interval.

I tried these two methods to set the time


0-59/45 * * * * * * * * exec /appl/prod/bin/test.sh >/dev/null 2>&1
*/45 * * * * * * * * exec /appl/prod/bin/test.sh >/dev/null 2>&1

and in both cases getting this error - crontab: error on previous line; unexpected character found in line.



Please suggest. Thanks in advance.

 
cron doesn't know intervals, only set times. You'll need multiple cron lines.

[tt]
0 0,3,6,9,12,15,18,21 * * * /path/to/job.sh
45 0,3,6,9,12,15,18,21 * * * /path/to/job.sh
30 1,4,7,10,13,16,19,22 * * * /path/to/job.sh
15 2,5,8,11,14,17,20,23 * * * /path/to/job.sh
[/tt]

Adjust the minutes if you don't want to start at 0:00 but e.g. at 0:10 like so:

[tt]
10 0,3,6,9,12,15,18,21 * * * /path/to/job.sh
55 0,3,6,9,12,15,18,21 * * * /path/to/job.sh
40 1,4,7,10,13,16,19,22 * * * /path/to/job.sh
25 2,5,8,11,14,17,20,23 * * * /path/to/job.sh
[/tt]

And you have way too many stars, cron will complain about that too ;-)


HTH,

p5wizard
 
So in this way I can set for any time interval. Can you please send me the syntax for 55 min time interval?

Your advice is helpful.
 
Can't you read a clock? ;-)

And 55 will be impossible for cron, as you need something that comes around to exactly the same time after 24 hrs (24*60=1440 minutes). Or if you specify a different schedule for every weekday: 7x24x60=10080 minutes, so 56 minutes would work (180 periods of 56 minutes = 10080 minutes)

[tt]
sun 0:00 0:56 1:52 2:48 ... 22:24 23:20
mon 0:36 ...
tue
wed
thu
fri
sat ... 21:12 22:08 23:04
[/tt]

But a shell script, run by [tt]at[/tt] and that re-schedules itself through the at command when it terminates may be more appropriate - see [tt]man at[/tt]


HTH,

p5wizard
 
As a matter of interest why is the interval 55 minutes in this thread, but 45 in the Solaris thread?

The internet - allowing those who don't know what they're talking about to have their say.
 
No problem p5, I was just interested.

The internet - allowing those who don't know what they're talking about to have their say.
 
P5/Ken,

I am unable to understand the meaning of the two command in unix
$var#pattern
$var##pattern
$var%pattern
$var%%pattern
What I read on the internet is
$var#pattern - remove the shortest match prefix pattern
from string.
$var##pattern - remove the longest match prefix pattern from string.
for exp
$var="/data/bin/prod/test.ksh"
then $var##*/ will return the filename test.ksh

$var%pattern - remove shortest match suffix pattern from string.
$var%%pattern - remove longest match suffix pattern from string.
for exp
$var%/* will return - path /data/bin/prod

I want to know what does the meaning of suffix and prefix.
Can you please explain with basic examples.

Thanks in advance.



 
Please don't tag a different question onto another thread, start a new one instead.

And you already found a basic example?

Code:
var="/data/bin/prod/test.ksh"
${var#*/} is value of var with shortest */ match removed i.e. "bin/prod/test.ksh"
${var##*/} is value of var with longest */ match removed i.e. "test.ksh"

Likewise for % and %%

Code:
${var%/*} is "/data/bin/prod" (/* then matches "/test.ksh")
${var%%/*} is "" (empty string because /* then matches the whole value of var)

I think you need the curly brackets for this variable substitution to work.

HTH,

p5wizard
 
Just some comments... p5wizard have given a good solution which importantly enough is portable.

Some unices (especially Linux variants) might support syntax that include /<number> where ``/<number>'' specifies skips of the number's value through the range.

# Min Hour DayOfMonth Month DayOfWeek Command
# 0-59 0-23 1-31 1-12/name 0-7/name (0 or 7=Sun)

#Min H DoM Mnt DoW Command
*/45 * * * * myprogram

You can check the manual page for your unix variant:
man crontab
or
man 5 crontab


*/5 will run each 5 minutes
*/30 will run each half hour
Both these are within the range of 0-60 and add up to 60.
But how will */45 behave - and what do you expect ;)
Question: Will it run once an hour or each 45 minute ?


Related to the small note made by p5wizard concerning a script that reschedule itself by usage of the 'at' command, this should not be underrated. I have used this solution myself whenever I want difficult or impossible schedules to be made via cron.

Example:
#!/usr/bin/sh
# This is my script
.....(commands)....

# Reschedule myself
at now + 45 minutes <<EOF
$0 >/dev/null 2>&1
EOF

Hint on Question: How many times does 45 go up in 60 ...

/2r
 
Answer: The answer is.... perhaps more than once ;)

So - when does it run?

Normally * represents all values in the range (for minutes, the range is 00-60).
And the /<number> was how much to skip within this interval.

As such, the syntax says run each minute, starting at 00 and then skip the next 45 minutes, then run again.
Since adding yet another 45 minutes is out of range that complete the number of runs.

Example: Cron runtimes for */45 minute
00:00
00:45
01:00
01:45
02:00
02:45
...

2 times within the range (00-59) the program will run.
Interval will be 45-15-45-15-45-15

as 45+15 = 60

If you specified 05/45 the job would run start 00:05, then wait 45 min and run 00:50, but still twice with same interval as above.

If you specify the first run to start at 15 minutes past each hour (15/45) or later, it runs 00:15 and next time your out of range => once per hour - and 1 hour interval.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top