24

I want to create a cron job which executes daiy at assigned time and it restart the ubuntu server.

I try to add reboot only in cron, but it is not working, whereas it works when I try to execute it from CLI.

Please advise, what command should I add in cron, so that it reboot the server daily at specific time.

5
  • May I ask why you need to reboot a server on a daily basis? This is an indication to me you're doing things wrong. Probably.
    – gertvdijk
    Jan 7, 2013 at 13:04
  • 4
    @gertvdijk i've run into situations where you need to reboot a machine regularly. Proprietary software that gets a bad memory leak after ~30 hours of uptime, things like that.
    – jrg
    Jan 7, 2013 at 13:08
  • 1
    Gaming servers (cough) Minecraft (cough) often ramleak or somehow fork into hundreds of processes.
    – Kaz Wolfe
    Dec 23, 2013 at 21:45
  • this post is also useful askubuntu.com/questions/327015/…
    – kevin
    Jan 28, 2017 at 13:11
  • If the problem is that some application is leaking memory over time, a better solution wold be to just restart that application service.
    – Soren A
    Feb 12, 2020 at 9:44

2 Answers 2

41

You need to run the following command:

/sbin/shutdown -r now

with root pirviliges. The way to do it is to use root's crontab, not your user crontab. A sudo before the usual crontab command does that:

sudo crontab -e

Tip: You can switch the shell's standard editor for things like crontab and visudo with sudo update-alternatives --config editor and then select the editor of your choice.

Editing the crontab you should add the following line to your file:

# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command  

@daily root /sbin/shutdown -r now

One has to remove "root" if you edited with the command: sudo crontab -e.

The "@daily" here is a shortcut for every day at midnight (equivalent to "0 0 * * *"). By the way - why do you want a daily reboot?

EDIT - see https://help.ubuntu.com/community/CronHowto for the following: "Crontab commands are generally stored in the crontab file belonging to your user account (and executed with your user's level of permissions). If you want to regularly run a command requiring administrative permissions, edit the root crontab file: sudo crontab -e"

EDIT - thanks to @charlesbridge for his comment - edited answer to include the full path

3
  • 2
    With cron, its always safest to use full paths: /sbin/shutdown -r Jan 7, 2013 at 12:59
  • 2
    what is the root do between the time day setting and the command? shouldn't it just be @daily /sbin/shutdown -r now in the root users cronfile? TIA
    – Ominus
    Aug 19, 2014 at 21:35
  • 3
    The following command has the same effect: /sbin/reboot. May 31, 2016 at 10:36
1

in my situation the best working method was

systemctl reboot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .