Solved

When is it 3am in bash script

Posted on 2014-07-22
31
236 Views
Last Modified: 2014-07-27
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
Comment
Question by:projects
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 8
  • 5
  • +1
31 Comments
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40211659
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
 

Author Comment

by:projects
ID: 40211668
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
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40211719
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:projects
ID: 40211739
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
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40211993
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
 

Author Comment

by:projects
ID: 40212180
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
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40212244
For a reboot, there is no need to adjust the values.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40212302
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
 

Author Comment

by:projects
ID: 40212334
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40212348
"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
 

Author Comment

by:projects
ID: 40212354
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40212370
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
 

Author Comment

by:projects
ID: 40212380
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 75 total points
ID: 40212387
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40212391
I think "shutdown" is there, but not in your PATH.

Please check /sbin/shutdown and /usr/sbin/shutdown
0
 
LVL 14

Assisted Solution

by:ThomasMcA2
ThomasMcA2 earned 75 total points
ID: 40212412
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
 
LVL 48

Expert Comment

by:Tintin
ID: 40213074
Easiest way is via an at command, eg:

echo "/your/command" | at 3am

Open in new window

0
 

Author Comment

by:projects
ID: 40213110
@Tintin, the question was how to put this into a bash script. Thanks.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40213222
Easy

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

Open in new window

0
 

Author Comment

by:projects
ID: 40213226
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
 
LVL 48

Expert Comment

by:Tintin
ID: 40213238
Just use

echo "reboot" | at 3am

Open in new window


or

at 3am <<EOF
reboot
EOF

Open in new window

0
 

Author Comment

by:projects
ID: 40214087
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
 
LVL 48

Expert Comment

by:Tintin
ID: 40215861
You just put it anywhere you like in the script.   The at job is scheduled in the background and the script continues on.
0
 

Author Comment

by:projects
ID: 40215964
#!/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
 
LVL 48

Expert Comment

by:Tintin
ID: 40216019
That means you don't have the at command installed.

Do

yum install at

or

apt-get install at
0
 

Author Comment

by:projects
ID: 40216022
ok but that's how I would use it then? Just as I posted?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40218428
Yes, as you posted, but without the 'done' as you aren't doing it in a loop.
0
 

Author Comment

by:projects
ID: 40219991
So it would be done just like any variable at the start of my script for example

BOOTER=86400
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40223408
Not sure what you mean.

Are you meaning something like:

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

Open in new window

0
 

Author Comment

by:projects
ID: 40223422
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
 
LVL 48

Expert Comment

by:Tintin
ID: 40223503
It doesn't matter where you put it in the script.   Just put it where ever it logically go.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question