You may wonder - why should I shutdown the Linux box automatically? It depends upon your situation. For example, you are downloading couple of tar balls and you want to go home early. You can schedule a job to shutdown Linux after downloading is completed. Linux/UNIX/BSD/OS X comes with at and cron commands to automate task. Almost all common task can be automated using at command.
Common Tasks For at Command
- Shutdown/reboot the 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 want to shutdown Linux box automatically everyday 8 pm then you need to use cron instead of at command (see below for examples). Useful to schedule recurring events or daily events such as backup data, or check system security etc.
at Command Examples
Let us see how to shutdow a Linux automatically at 8 pm, type the command at 8pm and press [enter] key, then type halt followed by enter key. To save your job press CTRL+D.
# at 8pm
Try out the 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 the following command:
# /etc/init.d/atd start
Linux Cron job to Shutdown Linux server/Desktop system
First, login as the root user and at a shell prompt type crontab -e command so that you can add cronjob:
# crontab -e
# crontab -e -u root
Append the following entry to it to shutdown box at 20:00 hrs [24 hour close format]:
0 20 * * * /sbin/shutdown -h now
Save and close the file.
- man pages crond, crontab, at