How to: Shutdown Linux box automatically
You might wonder why would I shutdown my Linux box automatically :/? It is depends upon situation. For example your downloading couple of tar ball and you wanna go home. You can schedule a job to shutdown Linux after downloading is completed. Linux/UNIX/BSD comes with at and cron commands to automate task, common task that can be automated using at command:
- Shutdown/reboot system
- Email yourself files
- Send birthday remainders
at- Execute a task at a specific time. For example, shutdown computer, send birthday remainder etc. Useful to schedule one job at a time or a single future event.
cron – If you wanna shutdown Linux box automatically everyday 8 pm then you need to use cron instead of at command (see below for example). Useful to schedule recurring events or daily events such as backup, check system security etc.
Let us come back to our example, shutting down Linux automatically at 8 pm, type command at 8pm and press enter key, then type halt followed by enter key. To save your job press CTRL+D.
# at 8pm
at> halt
(Press CTRL+D)
You can also try out following utilities:
- atq - List the current at jobs pending.
- atrm - Used to remove pending at jobs.
at command accept fairly complex time specifications, for example:
Run job at 6am on monday:
at 6am monday
Run job in 5 minutes time:
at now + 5 minutes
Run job at 4pm but 3 days later:
at 4pm + 3 days
Run job at 10am on 31st July:
at 10am Jul 31
See the file /usr/share/doc/at/timespec for complete time specifications and read man pages of at, atq, atrm. Make sure you have atd service running, if not start it using following command:
# /etc/init.d/atd start
Linux Cron job to Shutdown Linux server/Desktop system:
(1) Login as root user and at shell prompt type crontab -e command so that you can add cronjob
# crontab -e
OR
# crontab -e -u root
(2) Append following entry to it (shutdown at 20:00 hrs [24 hrs format]):
0 20 * * * /sbin/shutdown -h now
(3) Save the changes and exit to shell prompt.
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Linux Disable the Ctrl-Alt-Delete shutdown keys
- Schedule Windows server to reboot or shutdown automatically
- Shutdown account to shutdown Linux server
- How to: Allow any users to Shutdown Linux server
- Linux Shutdown Command and Logfile
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: at_command, cron, crontab_command, future_event, job, reboot_command, reboot_system, recurring_events, remainders, shutdown_computer, shutdown_linux, shutdow_command, system_security, unix_bsd



Thank you.
Good Explanation..
continue putting information like this…!!
Thanks. I just scheduled my first cron job.
Thanks for explanation! Can you tell me what is command for just restarting system? By the way, do I have to always put this commands to root or I can also make cron from my account? (sorry, I am quite new in all this..)
Use Login as root user and type reboot to reboot box:
rebootI have to shutdown the ubuntu server at midnight but before shutdown the user get 5 minites before interrupt and if the user have permission to discard the shutdown and continue if working
I personally wouldn’t schedule a shutdown if i was downloading a few tarballs and the system needed to be off when it was complete, unattended.
what if the speed decreases and it doesn’t finish in time?
Sure, you could expect it to take long and give it lots of time… But what if it gets done early, wouldn’t it be better if it shut down after it was complete, and not wait for the set time?
best to do something like this, chain commands. so you tell it to download the file and then when its done, shutdown.
wget http://somehwere.com/path/to/some.file && haltnote: it has to be double & signs. single one will cause both commands to execute at the same time. double causes it to wait for the first one to exit (clean exit or not) before continuing.
i personally would probably use more along the lines of
wget http://somehwere.com/path/to/some.file 2>/home/kai/wget.log && haltso level 2 outputs (errors) are logged. if the file stopped downloading before it was done, i would then know why by reading the output of wget.log
scheduled shutdowns with at or cron are better for just having a system shutdown _nomatter what_ at said time. if you want your tasks to finish first, chain them.
Thank u for information. I have one RHAS4 server.
For this morning 8 am it should switched on and late night 11 pm it should shut down automatically. Using crontab we can easily shut down the system. But switch on and shutdown the linux system in different time , i could not find any commands or script.
please reply