Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
11-01-2016, 03:28 AM
|
#1
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Rep:
|
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.
Thanks
Zoli
|
|
|
11-01-2016, 04:05 AM
|
#2
|
Member
Registered: Aug 2008
Location: Yorkshire
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 629
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.
|
|
|
11-01-2016, 05:10 AM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
Or you could just use a lot of commas in your crontab.
|
|
|
11-01-2016, 06:24 AM
|
#4
|
Moderator
Registered: Aug 2002
Posts: 26,352
|
You can use ranges. Lots of information on cron can be found like https://help.ubuntu.com/community/CronHowto
*/10 19-23,0-4 * * * /my/script
Last edited by michaelk; 11-01-2016 at 06:25 AM.
|
|
2 members found this post helpful.
|
11-01-2016, 06:27 AM
|
#5
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,006
|
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.
|
|
|
11-01-2016, 06:57 AM
|
#6
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Quote:
Originally Posted by wpeckham
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
}
|
|
|
11-01-2016, 06:58 AM
|
#7
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Quote:
Originally Posted by michaelk
|
Thanks, if this works, I'll go with this one :-)
Sorry I don't like to rely on a while loop, and a break in it.
|
|
|
11-01-2016, 06:59 AM
|
#8
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
"flock" is also another way of ensuring that only one instance is running at a time.
|
|
|
11-01-2016, 07:02 AM
|
#9
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
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.
Last edited by kzo81; 11-01-2016 at 07:04 AM.
|
|
|
11-01-2016, 07:12 AM
|
#10
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Quote:
Originally Posted by Turbocapitalist
"flock" is also another way of ensuring that only one instance is running at a time.
|
thx
|
|
|
11-01-2016, 07:13 AM
|
#11
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Quote:
Originally Posted by wpeckham
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.
|
|
|
11-01-2016, 07:47 AM
|
#12
|
Moderator
Registered: Aug 2002
Posts: 26,352
|
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.
|
|
|
11-01-2016, 08:30 AM
|
#13
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
this date math mite be easier:
Quote:
Originally Posted by schneidz
convert the dates to seconds then re-convert the output:
Code:
[schneidz@hyper tmp]$ date +%s -d 'Jun 17 02:00'
1466143200
[schneidz@hyper tmp]$ date -d '@1466143200'
Fri Jun 17 02:00:00 EDT 2016
|
|
|
|
11-01-2016, 08:33 AM
|
#14
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
Last edited by Habitual; 11-01-2016 at 08:42 AM.
|
|
1 members found this post helpful.
|
11-01-2016, 01:51 PM
|
#15
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Quote:
Originally Posted by michaelk
|
That's great ! I didn't know about cron ranges, I'm testing it now, it seems rock solid for my script!
|
|
|
All times are GMT -5. The time now is 03:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|