On Ubuntu how do I use CRON to reboot my server automatically every night at midnight?

Hi all,

I have a ubuntu 14.04 x64 distribution on a Linode VPS with Apache 2.4.7 and mySQL server 5.5.

I would like to be able to reboot my server automatically every midnight (GMT - my server date is set to GMT).

Could anyone please assist? I have no experience with CRON or .sh scripts and I have only started using linux last month.

thanks in advance
badwolfffAsked:
Who is Participating?
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.

savoneCommented:
Enter the following command to edit the crontab file:

sudo crontab -e

Add the following line:

0 0 * * * reboot

or

0 0 * * * shutdown -r now
0

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
Manfred BertlManagerCommented:
I dare to ask: why would you have a need to restart your server every day? I think, thats one of the strenghts of Linux, that it is very stable and you don't have to restart things on your server. The goal of a server, IMHO, is, that it runs 24/7 with no downtime. So, a reboot would cause also a few minutes downtime.
0
Sandeep KhanagwalCommented:
Add the line in the end of your Crontab file. It will reboot your server in midnight as per your vps time.

@daily shutdown -r now

Save file and reboot crontab .

For further reading / detail about crontab use the below link .

http://www.ubuntututorials.com/use-crontab-ubuntu/
0
serialbandCommented:
I 2nd mbertl's concern.  There is no good reason to be rebooting a server every night anymore.  If you are having problem's with apache or MySQL, you should be fixing those instead of rebooting.

Also, while it makes little difference today, if you have an older server you should only use the reboot command in single user mode, as it sent a sigkill.  The init command gracefully exited the processes in the reverse order that they were started.  In newer Redhat you should use systmctl to reboot.
0
badwolfffAuthor Commented:
Thanks for all the advice and the working code guys. I tried the code and it works but I turned the task off for now.
I use linode and I just upgraded to the next best service they have on offer. It costs me 10$ extra every month but no more problems related to running out of resources.

I was trying to patch things up but as you guys pointed out, the whole point of using linux is that rebooting should be used as a last option since the server has hardly ever crashed since I began using it. So I went to the source of the problem: too many sites using up all the resources. Got more resources, all sorted :)
0
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
Linux Distributions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.