I would like to schedule a restart of my Ubuntu every 30 minutes. Is there any command or a graphical way to do it?
The best way to do this will depend on why you want Ubuntu to restart every half hour.
So I recommend editing your question to explain why you wish to do this.
Rebooting every 30 minutes, and warning users before each reboot:
Assuming people might be using the machine, either locally or remotely, it's best to avoid restarting Ubuntu from under them without any warning. Therefore, rather than scheduling the
To schedule a shutdown every half hour with a warning 5 minutes before, add this to
You don't actually have to add the fist line, which is a comment. I've included it for clarity--something like it is there already.
The command will always run on the half-hour mark, if the machine is up and running at that time. This will cause the shutdowns to be announced every half hour and performed at 5 minutes and 35 minutes past the hour.
Making it easier for administrators to prevent automatic reboots from happening at all:
You might want to set this up so that it's easy for an administrator to suspend all automatically scheduled reboots:
This schedules reboots the same way--every half hour, with five minutes warning--except that it will not schedule a reboot if a file called
I love doing this by telling the Session Manager we want to reboot. This can be done without root permissions, and we get a nice window that warns us that the system is going to be rebooted -even we can cancel the reboot if we like to.
The Graphical Way - Preferred Method
Leave the other options at their default values. Click on Add.
The Terminal Way - No Need for Additional Software
Run from the terminal:
Add this line:
Save & exit. Assuming you are using
Please notice this won't work if your DISPLAY is actually different from
Not using Gnome or Unity?
Both methods explained above depends on some gnome components, found both on Gnome sessions and on Unity. If you want to do this on another environments (such as Kubuntu's KDE, Kubuntu's LXDE...) you better replace the command with this one instead:
This won't ask for confirmation, and will restart inmediatly, but will work on all environments, assuming you have not manually uninstalled ConsoleKit, of course.
This tells the system to run the command
Yes I do, in fact I never on any of my systems allow user based crontabs (There are better ways of allowing users to do user level scheduled tasks) cron was designed from the very beginning purely for system automation and not for users to schedule ordinary tasks. Things such as log rotations (which still happens today)
Reboot MUST be run as root to work correctly, the alternative is to set it's sticky bit, so that when run as a regular user it actually executes as root and works as expected, but in doing that your then opening your server up to allowing regular users to reboot it at will.
You could even possibly automate a call to SUDO, but I need to dig into that one, not sure if you can automate the need for a password with SUDO (I don't use it often, i prefer to just drop straight to a root shell using SU)
If you set it up in system wide crontab then everything is run as root, so my statement is accurate (I just neglected to mention that the system wide one should be used)
As for your question "Why Wrap it in a script?" well, why not? If the OP puts it in a shell script, then at some point in the future needs to add to it, he simply adds to the script, instead of having to open crontab locate the job, remove it, replace it with a shell script, then write a script with the old + new in.
Over 20 years as a Sys Admin / Developer working with systems as far back as Ultrix / Solaris and even VAX has taught me one major point.
If you can make it easier at the beginning, then it remains easy for it's life time.
I really don't get this "Minimalistic" attitude that a lot of modern day sys admins have, where doing as little as possible is the key to success. Most servers these days are easily 20 times+ more powerful than anything I ever started out on, and this kind of scenario (Wrapping in shell scripts) was recommended practice then, so there's really no argument for not doing it now.
Unless you really want to go hardcore Unix/Linux, in which case tag it all onto the cron entry, and pipe everything together the way it ought to be done :-)
However, I do digress, and also understand that many guys these days a thrown in at the deep end and told to make things work, as such they lack the time (and usually the inclination) to sit down and learn about new techniques (or old in this case) or even want to play with this stuff outside of work.
I personally, have a single server among the ones I run that's dedicated purely for me to play around on, so I can test things like this... which is better A or B, so it's not without reason that I advise on any of this.