Linux reboot at time

How can I schedule a nightly reboot?

I’m having some periodic issues running a particular application, XBMC. If I use XBMC regularly I don’t seem to have any issues but if I leave it unattended for more than about 12 hours I need to reboot to get it working again. I know a scheduled reboot is NOT the answer but until I can figure out the problem I need to schedule a reboot each morning so my wife can use it if I’m away and it doesn’t get thrown out the window 🙂 Any takers?

6 Answers 6

I’d use cron (should already be installed):

The first time you might have to choose your preferred editor (like nano)

at the bottom. Explanation:

m h dom mon dow command minute hour dayOfMonth Month dayOfWeek commandToRun 

would reboot your system every day at 4:05am. (4:00am + 5 minutes)

Ctrl + X , Y , Enter should get you out of crontab (if using nano)

Note: you might have to run crontab -e as root, because shutdown needs root. crontab -e opens a file in /tmp instead of the actual crontab so that it can check your new crontab for errors. If there are no errors, then your actual crontab will be updated.

Thanks for the info. The only thing I had to do was change the command to /sbin/shutdown to get it to work for me.

this didnt work for me. using both crontab -e and sudo crontab -e saves the file to a temporary location when you go to exit (e.g. /tmp/crontab.Y88XQl/crontab) and the task does not run. i was forced to edit /etc/crontab directly

@wal «crontab -e opens a file in /tmp instead of the actual crontab so that it can check your new crontab for errors and prevent you from overwriting your actual crontab with those errors. If there are no errors, then your actual crontab will be updated.» unix.stackexchange.com/questions/197504/…

Maybe it will help someone: I had to use sudo before reboot command in the file opened by sudo crontab -e in Linux Mint (but I’m not sure if it makes any difference in this case).

Adding this to /etc/cron.daily/zz-reboot should work:

And sudo chmod a+x /etc/cron.daily/zz-reboot . The «zz» prefix will force it to run last out of all the other scripts in that directory. Check /etc/crontab to see what time of day that will actually happen:

grep daily /etc/crontab | awk '' 

If that won’t work, then a «regular» cron entry can work too, via sudo crontab -e

MINUTE HOUR * * * shutdown -r now 

And finally, if you want to just do one-off reboots, you can use at :

echo "shutdown -r now" | sudo at 04:30 

as far as I know this will reboot your system the same time you created this file every day, please correct me if I’m wrong

Читайте также:  Firefox clear cache linux

Not quite: it will reboot once per day at whatever time cron.daily runs, which is a bit unpredictable. If you don’t have anacron installed, it’s 06:25 local time, which might be ok (or might be awful if you’re an early riser). If you do, which I think is the default, it could well be 10 minutes after the machine turns on for the first time each day, which would be infuriating. At any rate, cron.daily is not a good fit for this.

cron.daily run every night at 6:25 on my system. This is usually set to a random minute between 06:00 and 06:59. See your /etc/crontab for the time on your system

The biggest problem with using cron.daily for this is that it might interfere with other jobs in cron.daily.

I have been working with cronjobs for about a month at my work and scheduling poweroff, and reboot. It’s very simple. I know this was asked about 5 years ago, but if anyone still has problems, you can use this method and you will be set.

scroll all the way to the bottom and enter the below command

this is set for reboot at 6am everyday, and press enter

If you want to schedule poweroff at 11pm everyday you can enter

I still need to figure out how to poweron a machine using cronjob when it’s down. I will edit this answer once i figure it out.

P.S. this is my first ever answer posting on any forms; hope it helps someone!! 😀

There is an answer with the same content from 5 years ago with more detail and automatic syntax verification (through crontab -e ).

I know this is many years after the original message, however, its the first time I’ve come across it. The above line about using CRON to power on a server when its been powered down made me chuckle.

This is an older question, but it comes up as search result and does not contain any info about systemd, so I will add an example on how to do scheduled reboots with systemd. Filenames and unit descriptions in the example are chosen arbitrarily and can be changed.

    Add a service unit that restarts the system. To do that, create a new file in /etc/systemd/system/sched-reboot.service and add the necessary configuration into it. The following service config will run a forced reboot through systemctl whenever started.

 [Unit] Description=Scheduled Reboot [Service] Type=oneshot ExecStart=/usr/bin/systemctl --force reboot 
 [Unit] Description=Reboot Scheduling [Timer] OnCalendar=*-*-* 4:00:00 [Install] WantedBy=multi-user.target 

For more info about timers and the syntax for setting calendar events (as in OnCalendar in the example) have a look at the ArchWiki-section about Timers

Источник

How to Schedule Reboot Daily in Linux

Reboot the system means turning off all the applications of the device and restart with a new state. We need to reboot when the system crashes and fails to work correctly. Whenever the system restarts, it comes with a fresh copy of the software and is loaded into the memory.

Читайте также:  Перенаправление портов в линуксе

For Linux users, it is always easy to find the solution of operations you’re confused about. Similarly, if you want to reboot the system, you don’t need to install any specific tool or package.

Some of the processes also require rebooting the system for the configurational changes. It is good to reboot the system when needed, but doing it daily could create problems. So be careful!

The Crontab, or we can call cron table, is the built-in Linux utility that helps to schedule the processes and execute them at the specified time. The Cron table consists of predefined scripts and commands from where the Cron daemon reads them. The Cron allows the user to configure the predefined commands in the Cron table and set them to run automatically.

Some tasks need to perform daily, while some weekly, and similarly monthly and yearly.

Follow the guide if you want to schedule a reboot daily on the system:

How to Schedule Reboot Daily using Crontab

Following is the format of Crontab:

Fields Allowed Value
MIN (minutes) 0 – 59
HOUR (hour field) 0 – 23
DOM (day of month) 1 – 31
MON (month field) 1 – 12
DOW (day of the week) 0 – 6
CMD (command) Command to be executed

Open a terminal and type the mentioned command to get /etc/crontab file:

The file will look like this:

You can also get it using the command:

(You will be asked to select a preferable editor such as vim, nano, etc. for crontab file)

Scroll down the file and write the mentioned line at the end of it:

The above line will force the system to restart daily at 10 a.m.

Note that: The shutdown –r command is used to restart the system.

Conclusion

The writeup has shown how to reboot the system daily. You can do it by typing the shutdown -r command regularly on the terminal. However, to avoid writing the shutdown command daily, you can do it through Crontab.

The Crontab utility helps to schedule processes and execute them on time. All the predefined commands are written in the Cron table from where Cron daemon reads them. To schedule a process, you can edit the /etc/crontab file and set timings according to the requirement.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

How do I configure Ubuntu to reboot every day at a given time?

Using the terminal or a bash file, how can I configure Ubuntu to reboot every day at (say for instance) 3 AM? In other words, I want to schedule an automatic reboot through the terminal. I haven’t been able to find anything related to this on Google or AskUbuntu, and I’m fairly new to Ubuntu in general (just got my new Raspberry Pi).

Читайте также:  Linux скорость подключения интерфейса

3 Answers 3

Edit the crontab entries using crontab -e command (by default this will edit the current logged-in users crontab) and add the following line:

0 3 * * * echo $PASSWD | sudo -S reboot #change $PASSWD with your password 

Save the file and check the new crontab entry with crontab -l .

as crontab, this doesn’t work normally and you should check this post to see how can you make it to work.

Or, simple add the crontab to the root user’s crontab file offering the complete path for the reboot command using sudo crontab -e :

I didn’t know this. Is this a bug of cron or is it normal ? I mean, if you edit the crontab as root, then the job will executed only if you are logged in as root ? I think this is wrong. (or should be).

@OrangeTux You have right, sorry, I said that because I tested without to give the complete path for reboot command. Otherwise will not work. See my new edits.

Note that putting your clear-text password in a text file is not a good idea, so it’s best to have this job run as root from the get-go. Usually, rather than editing root’s crontab via the crontab command, which leaves the entries in /var/spool/cron/crontabs , a somewhat cryptic location, I prefer to enter them explicitly in /etc/cron.d . Entries in cron.d are run as system crontab entries, are treated as config files so they should survive system reboots, updates and upgrades, and you can explicitly specify the running user:

echo "0 3 * * * root /sbin/shutdown -h 5 'System will reboot in 5 minutes'" | sudo tee /etc/cron.d/reboot-at-3-am 

If you don’t need a specific time, but rather, just want the system to reboot once daily, add an executable or script in /etc/cron.daily and it will be automatically run at a predetermined time (6:25 AM system time by default):

echo "/sbin/shutdown -h 5 'System will reboot in 5 minutes'" | sudo tee /etc/cron.daily/reboot-me 

Notice that rather than just rebooting the system without warning, I’m setting a 5-minute warning, so if anyone is logged in, they have a chance to save their work, or even interrupt the shutdown with sudo shutdown -c , rather than having the system pulled off from under them. You can adjust these accordingly, if you want to give more ample warning (for instance, use shutdown -h 60 and run the command at 2:00 AM and you’ll give users a generous 1-hour warning).

This is based on my past experience; at some point you will be logged in working when the crontab entry runs, and if it just reboots without warning you’ll be a very sad panda.

Источник

Оцените статью
Adblock
detector