Previous Section  < Day Day Up >  Next Section

Recipe 7.12. Shutting Down Automatically

7.12.1 Problem

Your users are careless watt-wasters who refuse to develop the habit of shutting down their PCs at night, or maybe you think it would be nice to have your machine turn itself off at night, so you can just walk away and not worry about it.

7.12.2 Solution

It's easy as pie, thanks to cron. Add this line to /etc/crontab to automatically shut down your machine every night at 11p.m.:

#  m   h   dom mon dow   user    command

  00  23    *   *   *    root    /sbin/shutdown -h now

7.12.3 Discussion

/etc/crontab is perfect for simple cron setups. Note that it has a name field, so any user can have entries in this file. However, only root can edit /etc/crontab.

Another way is to use the crontab command:

# crontab -u root -e

This opens the root user's crontab. Edit and save, and you're done. Don't try to name the file yourself—during editing, it's a /tmp file, which is automatically renamed by crontab when you save it. It will end up in /var/spool/cron/crontabs, or somewhere close by.

7.12.4 A Brief Word on Uptimes

You've probably noticed Linux users bragging and obsessing about their uptimes. There's even an uptime command. There is nothing sacred about uptimes. Yes, I too love that Linux is stable as a table, and that most software installations and system upgrades never require a reboot. But let's not go nuts. If a particular machine does not need to be running 24 7, turn it off. It saves electricity, reduces the risk of something bad happening, and extends the life of your hardware.

7.12.5 See Also

  • cron(8), crontab(1), crontab(5)

    Previous Section  < Day Day Up >  Next Section