LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 11-01-2016, 03:28 AM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Rep: Reputation: Disabled
Smile 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
 
Old 11-01-2016, 04:05 AM   #2
petelq
Member
 
Registered: Aug 2008
Location: Yorkshire
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 629

Rep: Reputation: Disabled
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.
 
Old 11-01-2016, 05:10 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
Or you could just use a lot of commas in your crontab.
 
Old 11-01-2016, 06:24 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,352

Rep: Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148
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.
Old 11-01-2016, 06:27 AM   #5
wpeckham
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

Rep: Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840Reputation: 2840
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.
 
Old 11-01-2016, 06:57 AM   #6
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
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
}
 
Old 11-01-2016, 06:58 AM   #7
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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
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.
 
Old 11-01-2016, 06:59 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
"flock" is also another way of ensuring that only one instance is running at a time.
 
Old 11-01-2016, 07:02 AM   #9
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-01-2016, 07:12 AM   #10
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
"flock" is also another way of ensuring that only one instance is running at a time.
thx
 
Old 11-01-2016, 07:13 AM   #11
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
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.
 
Old 11-01-2016, 07:47 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,352

Rep: Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148Reputation: 6148
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.
 
Old 11-01-2016, 08:30 AM   #13
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919
this date math mite be easier:
Quote:
Originally Posted by schneidz View Post
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
 
Old 11-01-2016, 08:33 AM   #14
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...-month-524720/ offers clues, I think.

and some other notable hits with similar clues at
https://www.google.com/search?q=goog...rg%2Fquestions

Have fun!
Edit:
http://www.linuxquestions.org/questi...-a-4175511917/
seems like a better fit?

Last edited by Habitual; 11-01-2016 at 08:42 AM.
 
1 members found this post helpful.
Old 11-01-2016, 01:51 PM   #15
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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
That's great ! I didn't know about cron ranges, I'm testing it now, it seems rock solid for my script!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] From 2 days ago, it takes 10 min.s to boot my HP dv9700 laptop. Prior two yr.s, 2 min PTrenholme Linux - Hardware 6 12-28-2010 12:40 PM
Need to be able to set cron to reboot system every 5 min Thaidog Linux - General 1 07-19-2010 04:08 PM
Error - Why cron job running every 1 min ravibhure Linux - Newbie 7 06-11-2009 07:28 AM
NTP cron job runs every 5 min instead of hour OlRoy Linux - Networking 17 02-28-2008 11:51 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration