LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2020, 09:59 PM   #1
Sloth77
LQ Newbie
 
Registered: Dec 2020
Posts: 3

Rep: Reputation: Disabled
Bash timeframe


.

What’s the best way to specify a time and day for which to defer a command?

So if I want to defer any command that comes in between 5 and 6pm Sunday through Thursday….I know I need to sleep for 60m but how do I specify the day/time for when to start and end the deferment? Thank you
 
Old 12-30-2020, 12:04 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
man at
Or maybe
man cron

Tho I’ll admit that “command that comes in” isn’t at all clear...

Last edited by scasey; 12-30-2020 at 12:06 AM.
 
Old 12-30-2020, 12:35 AM   #3
Sloth77
LQ Newbie
 
Registered: Dec 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
man at
Or maybe
man cron

Tho I’ll admit that “command that comes in” isn’t at all clear...
I had thought about Cron but the issue with this are alerts won’t always come in between 5-6pm, but if they do I need the bash file to NOT execute until 6:01pm.

I’m only familiar with how to use cron to perform a function at a certain time.
 
Old 12-30-2020, 02:38 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Ah. Do you already have a script that processes the alerts? Use at (man at) inside that script to control when to execute the processing.

Hard to be mor specific without more details.
 
Old 12-30-2020, 06:30 AM   #5
Sloth77
LQ Newbie
 
Registered: Dec 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Ah. Do you already have a script that processes the alerts? Use at (man at) inside that script to control when to execute the processing.

Hard to be mor specific without more details.
Yes that is correct. I have a script that does everything it is supposed to do 24/7 for incoming alerts. Now I just need to add in a couple lines to still accept incoming alerts between 5 and 6pm each day but defer the reaction to those alerts till 6:01pm each day.
 
Old 12-30-2020, 06:58 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

You could use the at command - at '16:01pm'

Another option is to just calculate the number of seconds to sleep. Using timestamps (seconds since 1970-01-01 00:00:00 UTC) and the date command, it's very easy to calculate.

Code:
ts1=$(date '+%s')
ts2=$(date -d '6:01pm' '+%s')
seconds=$(($ts2-$ts1))
sleep $seconds && ...
Both the at command and date use today as default.
 
Old 12-31-2020, 04:03 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
I think the date command plus some conditions are needed here. No crontab or at.
Set multiple variables by one date command
Code:
eval $(date +"hour=%H dow=%u")
if
  [ $hour -ge 17 -a $hour -le 18 ] &&
  [ $dow -le 4 ] 
then
  sleep 3600
fi
Consult
Code:
man date
for values and parameters.

There might be a difficulty with a leading 0 being interpreted as octal number.

Last edited by MadeInGermany; 12-31-2020 at 08:18 AM.
 
Old 12-31-2020, 06:36 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
probably a flag? You will stop "the reaction to those alerts" when it was set and unset it at 6:00. I still do not understand when and how should it be set.
 
Old 12-31-2020, 07:11 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
So the script must start at 5am and must execute its final command after 6am, correct?
And you already know how to start it at 5am, correct?
The rest should be easy, maybe post #6.
Also, for added code golf: you can use the $SECONDS variable instead of date. 'man bash'
 
  


Reply



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
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
LXer: Standards: Have Any Suggestions for the Next Version of ODF?(timeframe : may 1 LXer Syndicated Linux News 0 02-24-2009 09:30 AM
Yum runs once or twice within a small timeframe and then will not run until a reboot. KTheorem Linux - Software 1 11-27-2006 05:53 AM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 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