How to Cron a script from X pm to Y am in every Z min
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to Cron a script from X pm to Y am in every Z min
Hi All,
Please, tell me how can I do this:
I have a download script, and I'd like to schedule it with cron.
I'd like to have it run from 19:00 till 05:00 in every 10 minutes.
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 619
Rep:
You would amend you script to operate in a while ... next loop and include sleep 600 after the actual script and before the "next".
You can then install in your crontab for 19.00 everyday.
I would add features to your script to detect if a copy is already running. If anything made it run long, or not exit, you could otherwise easily end up with MANY copies in memory running at the same time with inconvenient consequences.
I would add features to your script to detect if a copy is already running. If anything made it run long, or not exit, you could otherwise easily end up with MANY copies in memory running at the same time with inconvenient consequences.
I thought of that, thanks, this way they wont run into each other if 10 minutes was not enough for the downloading:
function no_multiple_instance(){
if [ "$(pgrep -x $(basename $0))" != "$$" ]; then
echo 'only one instance is allowed'
exit 1
fi
}
in the meantime I was thinking of a function that exits early the scipt if it's not in the time interval, and have the script run repeatedly with cron...
but it's still buggy:
#!/bin/bash
FROM_HOUR=19
FROM_MIN=05
TO_HOUR=4
TO_MIN=05
let NOW=`date +%-H`+`date +%-M`
#let NOW=`date +%-M%-H`
let FROM=$FROM_HOUR+$FROM_MIN
let TO=$TO_HOUR+$TO_MIN
function checkTime(){
# exit early function if not in the time interval
if [ $FROM_HOUR -gt 23 ] || [ $TO_HOUR -gt 23 ] || [ $FROM_MIN -gt 59 ] || [ $TO_MIN -gt 59 ];then
echo "Specify hours in between 0-23 and minutes in between 0-59 !"
exit 1
fi
if [ $FROM -lt $NOW ] ;then
echo "before"
exit 1
fi
if [ $TO -gt $NOW ] ; then
echo "after"
exit 1
fi
}
checkTime
But do this with cron, even if it's a bit complicated is more sympathetic to me.
I would add features to your script to detect if a copy is already running. If anything made it run long, or not exit, you could otherwise easily end up with MANY copies in memory running at the same time with inconvenient consequences.
thx, but I don't like a while loop with a break in my script.
If a loop is written correctly there is no reason you can not rely upon it. bash does not automatically do time math and you would typically convert hours-minutes to seconds depending on your requirements.
Quote:
NOW=`date +%-H`+`date +%-M`
NOW=$( date +%-H%M )
By using the %-M you will remove any leading zeros which could lead to bad results.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.