Solved

When is it 3am in bash script

Posted on 2014-07-22
31
229 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
  • 13
  • 8
  • 5
  • +1
31 Comments
 
LVL 14

Expert Comment

by:ThomasMcA2
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:projects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
For a reboot, there is no need to adjust the values.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I think "shutdown" is there, but not in your PATH.

Please check /sbin/shutdown and /usr/sbin/shutdown
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Assisted Solution

by:ThomasMcA2
ThomasMcA2 earned 75 total points
Comment Utility
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
Comment Utility
Easiest way is via an at command, eg:

echo "/your/command" | at 3am

Open in new window

0
 

Author Comment

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

Expert Comment

by:Tintin
Comment Utility
Easy

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

Open in new window

0
 

Author Comment

by:projects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
That means you don't have the at command installed.

Do

yum install at

or

apt-get install at
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:projects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It doesn't matter where you put it in the script.   Just put it where ever it logically go.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now