bash script execute something at certain time

I have a bash script running some code 24/7. I am currently using a timer in the script to reboot the machine every 43000 seconds. This is not working well because each time the machine fires up the script again, it reboots over and over until a certain amount of time has passed.

I don't know why but I would prefer to check the clock and reboot at some set time such as 3am.

What code would I put into my script which would check the time and when it is 3am, it would run a command.
My function currently looks like this. There is a loop which runs this function every 43000 seconds.

function daily_reboot()
{
        reboot -f
}
projectsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
The Linux "shutdown" command accepts a time parameter. In your case you would simply put in your script (without any loop around it):

shutdown --no-wall -r 3:00

This will shutdown and reboot the machine at 3 AM. No messages will be issued.

See "man shutdown" for more options of the "shutdown" command.
projectsAuthor Commented:
So in other words, just put it at the top of the script so it is run once and not in the loop.
I now think I asked this question before and someone said the same thing :)

Only problem was, this is an openwrt router so there is no shutdown command.
There is a reboot command however.

# reboot -h
reboot: invalid option -- h
BusyBox v1.22.1 (2014-09-20 22:01:35 CEST) multi-call binary.

Usage: reboot [-d DELAY] [-n] [-f]

Reboot the system

        -d SEC  Delay interval
        -n      Do not sync
        -f      Force (don't go through init)
arnoldCommented:
A server that is being rebooted on a set frequency suggests there is another issue that is not being addressed.
What is the issue that leads/requires a reboot on this frequency?

IMHO, an unattended reboot leads to situations where it either loops, or gets to a point that it will not boot at all.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

projectsAuthor Commented:
Actually, it is simply a remote recovery failsafe, making sure the device reboots now and then because the script needs to restart and re-read some things.

Either way, I do need it to reboot daily at 3am :)
woolmilkporcCommented:
Please check if your machine supports the "at" command. If so, you can use (again, without any loop):

at 3:00 <<EOF
reboot -f
EOF
arnoldCommented:
Since you want a reboot at 3:00 am, you would need to perform some meaning get the current date/time, and then determine how many seconds till 3:00 am.
projectsAuthor Commented:
Yes, I could add 'at' but I no longer have access to the devices to add software.
The only thing I can do is remotely boot them so they will restart and pick up new software but that is the catch right now.

I can only reach them using bash scripting as they are all remote to me. They will restart in some 12hrs so I need to make sure I've added by reboot code into the script.
woolmilkporcCommented:
How about a cron job?
woolmilkporcCommented:
How about a cron job?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
A script running that periodically checks your URL for a specific pattern on which the device robots itself
To avoid cyclical, your config needs to alter the pattern it looks for
I.e. Step one look for 123boot while in the new config change the loom for pattern to 321boot.....
projectsAuthor Commented:
Yup, cron job will do the trick but I'll stick it in the script.

## Setting the crontab to reboot the machine at 03:01
        echo -e "#### auto reboot ####\n01 03 * * * reboot" > /tmp/crontemp
woolmilkporcCommented:
/tmp/crontemp" will not be seen by the cron daemon, at least not on systems I'm aware of.

You could check if the current crontab already contains a reboot command, and if it doesn't
add it by means of the "crontab" command, saving what's already in the table.


if ! crontab -l | grep -q "reboot"
   then
     crontab -l > /tmp/crontemp
     echo -e "#### auto reboot ####\n01 03 * * * reboot" >> /tmp/crontemp
     crontab /tmp/crontemp
fi
projectsAuthor Commented:
Sorry, the whole code is;

## Setting the crontab to reboot the machine at 03:01
                echo -e "#### auto reboot ####\n01 03 * * * reboot" > /tmp/crontemp
                crontab /tmp/crontemp
                rm -fv /tmp/crontemp

Not sure how to do it more elegantly but this works.
woolmilkporcCommented:
Should be sufficiently elegant, but I hope you're aware that the current content (if any) of your crontab will be lost!

Thx for the points!
projectsAuthor Commented:
It's the only crontab so good to go :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.