nixCraft Poll

Topics

How to: Shutdown Linux box automatically

Posted by Vivek Gite [Last updated: September 13, 2007]

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:

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:

Discussion on This Article:

  1. arun Says:

    Thank you.

  2. Ganesh Says:

    Good Explanation..

    continue putting information like this…!!

  3. seren6ipity Says:

    Thanks. I just scheduled my first cron job.

  4. kana Says:

    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 Says:

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

  6. Rahul Hulawale Says:

    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 Says:

    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 Says:

    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

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!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.