Difference between revisions of "Cron"
(add highlight about ntp) |
(Add info about one-time jobs using AT) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Cron is the system scheduler on Unix/Linux/Mac operating systems. The ''crontab'' is the schedule of actions that the machine must keep. There are two types of crontabs in place on most systems: ''user crontabs'' and the ''system crontab''. | Cron is the system scheduler on Unix/Linux/Mac operating systems. The ''crontab'' is the schedule of actions that the machine must keep. There are two types of crontabs in place on most systems: ''user crontabs'' and the ''system crontab''. | ||
− | |||
− | |||
== Avoid User Crons == | == Avoid User Crons == | ||
Line 22: | Line 20: | ||
* <code>man crontab</code> can provide a lot more information. | * <code>man crontab</code> can provide a lot more information. | ||
− | < | + | == One-time Jobs == |
− | + | You can certainly use cron to run one-time jobs, but it would seem to be more effort than it's worth, since cron is designed for '''repeating''' jobs. | |
+ | |||
+ | Using the <code>at</code> command, you can specify a job to be run at some time in the future. The most common thing I do with <code>at</code> is schedule a reboot. Of course if you just want to reboot the machine at 10pm, you can do <code>/sbin/shutdown -r 22:00 "rebooting at 10:00 P.M." &</code> Do this in a [[screen]] session to ensure that the current shell does not need to be intact, but I think backgrounding the process with '''&''' takes care of that (depending on your shell environment). Anyway, the <code>shutdown</code> command is not as flexible with date and time arguments as the <code>at</code> command. With <code>at</code>, you can specify <code>at 8am tomorrow</code>. Invoking this command launches an interactive shell which you then enter the command to be executed. E.g. <code>shutdown -r now</code>. Finally, you exit the interactive shell with <kbd>Ctrl</kbd>+<kbd>d</kbd>. <code>atq</code> will list the jobs in the queue. <code>at -c 1</code> will 'cat' (display) details of job '''1''' <code>atrm 1</code> will remove job #1 from the queue. | ||
+ | |||
+ | Since <code>at</code> reads from STDIN, you can just pipe the command to it rather than using the interactive shell: <code>echo "ls -al" | at now + 1 minute</code> Note that since <code>at</code> runs non-interactively with a different shell, you won't actually '''see''' the results of the 'ls' command. | ||
+ | |||
+ | <source lang="bash"> | ||
+ | echo "shutdown -r now" | at 8am tomorrow | ||
+ | echo "shutdown -r now" | at midnight 2017-02-25 | ||
+ | # show the queue | ||
+ | atq | ||
+ | # show details of job 1 | ||
+ | at -c 1 | ||
+ | # cancel / remove job 1 | ||
+ | atrm 1 | ||
+ | </source> |
Revision as of 15:48, 24 February 2017
Cron is the system scheduler on Unix/Linux/Mac operating systems. The crontab is the schedule of actions that the machine must keep. There are two types of crontabs in place on most systems: user crontabs and the system crontab.
Contents
Avoid User Crons[edit | edit source]
System administrators who manage servers and services should emphasize system crons over user crons. User crontabs are generally found under /var/spool/cron/crontabs
and should be avoided in the context of managing cron for a server. They are intended for when a system is a multi-user system where individual users all may have regular maintenance scripts and commands that they manage themselves.
System Cron Standard[edit | edit source]
example.com servers should all be using system crontabs to manage the functionality that the server is responsible for. Specifically, this includes the files /etc/crontab
, the files under /etc/cron.d/
, and usually, the cron.hourly
, cron.daily
, cron.monthly
folders. All example.com-specific crontab entries should be made to the file /etc/cron.d/example.com
or /etc/cron.d/example.com-something
. If this file is not present, it should be created.
How to edit crontabs[edit | edit source]
- Use a Text Editor to create/edit
/etc/cron.d/example.com
and add entries for new schedule jobs. - Send notification to the "Core Tech" about the adjustment.
- Comment Make sure your crontab entries include generous comments about what it does, when it does it etc, when it was implemented or last changed etc. Do not rely on the user's ability to read (cryptic) cron systax! nor force the user to inspect the contents of each script. Do point to reference sources like wiki or support URLs for detailed information. Complex scripts should be written independently, tested and saved to /root/bin where they can be easily called from cron.
User crons[edit | edit source]
Using user crontabs is discouraged for servers, but common in personal workstations or development environments
General crontab hints[edit | edit source]
- Use
crontab -l
to list the crontab for the current user - Use
crontab -e
to edit the crontab for the current user man crontab
can provide a lot more information.
One-time Jobs[edit | edit source]
You can certainly use cron to run one-time jobs, but it would seem to be more effort than it's worth, since cron is designed for repeating jobs.
Using the at
command, you can specify a job to be run at some time in the future. The most common thing I do with at
is schedule a reboot. Of course if you just want to reboot the machine at 10pm, you can do /sbin/shutdown -r 22:00 "rebooting at 10:00 P.M." &
Do this in a screen session to ensure that the current shell does not need to be intact, but I think backgrounding the process with & takes care of that (depending on your shell environment). Anyway, the shutdown
command is not as flexible with date and time arguments as the at
command. With at
, you can specify at 8am tomorrow
. Invoking this command launches an interactive shell which you then enter the command to be executed. E.g. shutdown -r now
. Finally, you exit the interactive shell with Ctrl+d. atq
will list the jobs in the queue. at -c 1
will 'cat' (display) details of job 1 atrm 1
will remove job #1 from the queue.
Since at
reads from STDIN, you can just pipe the command to it rather than using the interactive shell: echo "ls -al" | at now + 1 minute
Note that since at
runs non-interactively with a different shell, you won't actually see the results of the 'ls' command.
echo "shutdown -r now" | at 8am tomorrow
echo "shutdown -r now" | at midnight 2017-02-25
# show the queue
atq
# show details of job 1
at -c 1
# cancel / remove job 1
atrm 1