• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

When is it 3am in bash script

In a bash script, I currently have an event which I need to run at 4am. Since I haven't found a way of doing this, I have instead been using a cron task which runs at 4am.

However, I need to do the same thing in the script, running something at 3am but how do I check to know what time it is? Running an hourly check or something? This without having the script continuously using up resources to check the time because it would be busy doing other things.

function three_am

do something now that it's three am
0
projects
Asked:
projects
  • 13
  • 8
  • 5
  • +1
2 Solutions
 
ThomasMcA2Commented:
If you need this to run regularly, use cron. If you only need it to run once, use the "at" command.

Either way, create two scripts - one for 3am and one for 4am.
0
 
projectsAuthor Commented:
As I said, I am using cron but I also need a means by which to run something at 3am from within the bash script.
0
 
ThomasMcA2Commented:
It seems like you would be better off creating another script, but I can't be sure because I don't know what your script(s) is(are) trying to do.

You could schedule your script for 3 am, then use "sleep 1h" to pause for an hour, then run your 4am code.
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
projectsAuthor Commented:
You are perhaps misunderstanding my question :)

I am wanting to know how someone would run a function at a specific time inside of a bash script which is already running a number of other things. In other words, I don't want it to sleep and wait because it needs to continue running other functions in the meantime.

If it all possible, I don't want the script to have a spend much resource time looking up the time but I do want it to do something at a specific system time.

Wondering how someone does that.
0
 
ThomasMcA2Commented:
Gotcha. Try this:
time="date +%k%M%S"
  if [[ $(eval "$time") -ge 30000 ]] && [[ $(eval "$time") -le 30005 ]];then
      # do your stuff
  fi

Open in new window


I don't know if it would run at exactly 3am, so I checked a 5-second range.

Obviously that needs to be inside a loop that will be running at 3am. If your loop takes less than 5 seconds to run, your code could get launched more than once (i.e., at 3am and again 3 seconds later.) Adjust the "greater than" and "less than" values accordingly.
0
 
projectsAuthor Commented:
I actually want to reboot the workstation at 3am in this case. Once I've had the chance to get access to it, I'll then set up a cron task. For now however, all I can do is put it inside a script and send it to it.

I do have a main loop which runs probably every second so I could put it inside of that.
Just not clear on your mention of adjusting the values however.
0
 
ThomasMcA2Commented:
For a reboot, there is no need to adjust the values.
0
 
woolmilkporcCommented:
Are you aware that "shutdown" has a "when" argument (and also a reboot flag "-r")?

shutdown -r 3:00 >dev/null 2>&1 &

should do the trick.
0
 
projectsAuthor Commented:
Yes, I can use the shutdown command as well as reboot but I just don' t know how to set that up in a script which is already running many other functions. I can't have the script sleeping, I need it to know what time it is so it can properly reboot at 3am
0
 
woolmilkporcCommented:
"shutdown" will know when it's 3AM. Please note the ampersand "&" at the end of the arguments.
This will create a background job, your main script will continue.
0
 
projectsAuthor Commented:
So you are saying simply put
shutdown -r 3:00

say at the start of my script and that's all I need to do? It's a reboot so I don't need to keep the script running. I want the machine to fully restart.
0
 
woolmilkporcCommented:
Yes, if the script does not have to be kept running after the timed "shutdown" command was issued.

 However, if the script must do other things until the reboot  time is reached please reread what I told you above  about the ampersand at the end of the command.
0
 
projectsAuthor Commented:
No need to keep anything going, a hard reboot is all I need.
The os doesn't have the shutdown command, it only has reboot.


reboot -h

        -d SEC  Delay interval
        -n      Do not sync
        -f      Force (don't go through init)
0
 
woolmilkporcCommented:
You can of course also "sleep" in the background:

function sleepuntil {
local D=""
[[ ! $(date "+%H:%M") < "$1" ]] && D="tomorrow"
sleep $(($(date -d "$D $1" "+%s")-$(date "+%s")))
echo Do "$2" here
}

sleepuntil "03:00" "reboot" &

The main script will continue after the function call. Again, please note "&" at the end!
0
 
woolmilkporcCommented:
I think "shutdown" is there, but not in your PATH.

Please check /sbin/shutdown and /usr/sbin/shutdown
0
 
ThomasMcA2Commented:
Then put reboot inside your loop, like this:

time="date +%k%M%S"
  if [[ $(eval "$time") -ge 30000 ]] && [[ $(eval "$time") -le 30005 ]];then
     reboot
  fi

Open in new window

0
 
TintinCommented:
Easiest way is via an at command, eg:

echo "/your/command" | at 3am

Open in new window

0
 
projectsAuthor Commented:
@Tintin, the question was how to put this into a bash script. Thanks.
0
 
TintinCommented:
Easy

#!/bin/bash
echo "/your/command"|at 3am

Open in new window

0
 
projectsAuthor Commented:
Uh? You are saying I could do something like. I've never seen anything like that.

reboot | at 3am

Can you show me an example using 'reboot' because that's what this machine users, no shutdown.
0
 
TintinCommented:
Just use

echo "reboot" | at 3am

Open in new window


or

at 3am <<EOF
reboot
EOF

Open in new window

0
 
projectsAuthor Commented:
Sorry, still not sure how to use this.
Do you mean just stick it anywhere like a normal variables

restart=some_variable
echo "reboot" | at 3am

or in a loop?

I just don't understand how this could work.
0
 
TintinCommented:
You just put it anywhere you like in the script.   The at job is scheduled in the background and the script continues on.
0
 
projectsAuthor Commented:
#!/bin/bash
echo "Does this work" | at 02:56:10
done

# ./test2
./test2: line 3: at: command not found
./test2: line 5: syntax error near unexpected token `done'
./test2: line 5: `done'
0
 
TintinCommented:
That means you don't have the at command installed.

Do

yum install at

or

apt-get install at
0
 
projectsAuthor Commented:
ok but that's how I would use it then? Just as I posted?
0
 
TintinCommented:
Yes, as you posted, but without the 'done' as you aren't doing it in a loop.
0
 
projectsAuthor Commented:
So it would be done just like any variable at the start of my script for example

BOOTER=86400
0
 
TintinCommented:
Not sure what you mean.

Are you meaning something like:

BOOTTIME=2:56
echo "reboot" | at $BOOTTIME

Open in new window

0
 
projectsAuthor Commented:
Yes, like a variable

But, I just meant in terms of where to stick it in the script :)
This is a really helpful tip. I appreciate it very much.
0
 
TintinCommented:
It doesn't matter where you put it in the script.   Just put it where ever it logically go.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 13
  • 8
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now