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:

  1. Shutdown/reboot system
  2. Email yourself files
  3. 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 read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 12 comments… read them below or add one }

1 arun 08.17.07 at 6:52 am

Thank you.

2 Ganesh 10.11.07 at 12:33 pm

Good Explanation..

continue putting information like this…!!

3 seren6ipity 02.17.08 at 8:46 pm

Thanks. I just scheduled my first cron job.

4 kana 03.31.08 at 12:55 pm

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..)

5 vivek 03.31.08 at 1:00 pm

Use Login as root user and type reboot to reboot box:
reboot

6 Rahul Hulawale 04.03.08 at 11:01 am

I 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

7 kai 05.14.08 at 5:51 pm

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 && halt
note: 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 && halt
so 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.

8 saroj 06.26.08 at 11:16 am

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

9 peachy 09.20.08 at 4:55 pm

@ saroj Waking up the computer is beyond the OS, its at the BIOS Level and needs an ATX Power supply too.

Some BIOS s dont have that capability (Old/some OEM)

but if found its usually under power management.

10 Anshul Shrivastava 05.15.09 at 8:41 am

I didn’t come across usage of shutdown but I have seen examples of user logged off from a machine at particular time. Like in computer training institutes, they used to fix machines for users for a certain duration of time. User comes and uses available machine and once his or her time gets over, it logs off.

11 Martijn 06.12.09 at 7:28 pm

Thanks! I was having trouble wrapping my head around the crontab thing. This showed how to do it (and by extension, how it works).

12 Jan van Leeuwen 06.19.09 at 11:03 am

Thank you,

untill now didn’t need crontabs, coming from VMS I could not get a hold on this crontab, since this Howto I understand at last, I have read loads of internetsites, man pages, books about it. I must say how can something so simple be described so complex.
I will visit your website more often!

Jan

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , , , ,

Previous post: Book review: Pro DNS and BIND

Next post: Improve DNS performance for Linux / Windows desktop using DNS caching software