LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-08-2015, 05:47 AM   #1
heathcliffz
LQ Newbie
 
Registered: Jul 2007
Location: Brazil
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 0
How to: Script run every 1 hour between 18h and 7h every day


Hey guys, how can I make a script run every 1 hour between 18h and 7h every day using cron?

Or... Is there other way without using cron on ubuntu based distros?

The script sends a mensage to the user saying that the PC will shutdown in 10 minutes, but the user can cancel and one hour later the script will ask again...

If you have any sugestions about how can I make this easier I would be happy to know!

Thanks!
 
Old 12-08-2015, 05:56 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Yes you can. See the man page for crontab http://man7.org/linux/man-pages/man5/crontab.5.html.
 
1 members found this post helpful.
Old 12-08-2015, 07:03 AM   #3
heathcliffz
LQ Newbie
 
Registered: Jul 2007
Location: Brazil
Distribution: Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
thanks

I let laziness aside and read the manual ...
from what I understand, I would look something like this:

Code:
0 18-7 * * * root echo "HI!"

or am I wrong?

Last edited by heathcliffz; 12-08-2015 at 07:14 AM.
 
Old 12-08-2015, 08:25 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Looks like you're right. The man page says "The specified range is inclusive. For example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11".

To console you, I spend 20% of my time reading manual pages. I can only remember the the stuff I use often.
 
1 members found this post helpful.
Old 12-08-2015, 08:55 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by heathcliffz View Post
thanks

I let laziness aside and read the manual ...
from what I understand, I would look something like this:

Code:
0 18-7 * * * root echo "HI!"
Cron may not understand that the range is supposed to wrap around midnight, and all versions of cron may not interpret that consistently. You should write that as
Code:
0 0-7,18-23 * * * root echo "HI!"
See https://www.google.com/search?as_q=cron+midnight+wrap

Also, while the format of that entry is correct for the system's /etc/crontab, where a user name is required for each entry, an individual user's crontab does not have that field:
Code:
0 0-7,18-23 * * * echo "HI!"
It's fortunate that you said you want to do this "every day." Doing it, e.g., "every day except Saturday night / Sunday morning" would be more complicated.

Last edited by rknichols; 12-08-2015 at 09:00 AM. Reason: Add format for individual user's crontab
 
1 members found this post helpful.
Old 12-08-2015, 09:02 AM   #6
heathcliffz
LQ Newbie
 
Registered: Jul 2007
Location: Brazil
Distribution: Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
To console you, I spend 20% of my time reading manual pages. I can only remember the the stuff I use often.
berndbausch,
I never read much about CRON because never needed something very specific, but now I read, I believe that I can think of something in the future...

Quote:
Cron may not understand that the range is supposed to wrap around midnight, and all versions of cron may not interpret that consistently.
rknichols,
Man, thank you! I had not thought about it!
 
  


Reply

Tags
cron, script, shell


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron run every hour except 6 am holyearth Linux - General 2 07-24-2012 11:48 AM
Start script at some day at some hour miros84 Linux - Software 4 02-14-2010 03:02 PM
How to run a shell script evey day automatically at a particular time krevathi1912 Programming 3 09-22-2007 01:15 PM
Time jumps by 1 day for an hour, then jumps back on RH 9? dieyouspammer Red Hat 3 04-07-2006 12:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:16 AM.

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