The crontab works much like cron.d, just with its own command for access and more flexibility when you want multiple users setting their own schedules.
Oldie but goodie: crontab
There's one more approach that can be taken when scheduling jobs with cron, and that's the "crontab". The crontab is the oldest and most standard approach to scheduling with cron, so you'll occasionally run into a package that installs a crontab entry. Using crontab can also provide a convenient method of allowing users to schedule events in cron without needing access to a file in one of the /etc/cron.* directories.
The crontab uses a scheduling entry format similar to what's used in /etc/cron.d. If you're not familiar with that format, you should go back to the previous article in this series to read up on cron.d and how it schedules tasks.
The crontab is something you'll typically access on a per-user basis.
Viewing the crontab
To view the crontab for the current user, run the crontab command with the "-l" option:
If you get a "no crontab" response, then you don't have a crontab set up for that user.
If you do see a crontab listing, you'll notice that each line will look an awful lot like the format we used for files in /etc/cron.d.
14 10 * * * /usr/local/bin/anotherprogram --option
The main difference is that there is no "user" field in crontab entries, since those are already set to run as the user that owns that crontab. So the first five entries in the line are still for scheduling, and the rest of the line is the command or script to be run.
Editing the crontab
To edit your crontab, use the "-e" option:
This will launch your default editor and drop you into a file that will either be blank or start with a commented-out line (a line starting with the "#" character) that lists the order of schedule entries. Once you make the changes you want (either editing existing lines or adding new ones), just save the file and it will be written to the crontab.
Working with other users' crontabs
As root, or with sudo, you can list or edit other users' crontab entries. This is done with the "-u" option, followed by the username, then the option telling crontab what you want to do:
sudo crontab -u demouser -l
The above command would list the crontab entries for user "demouser".
Who can use crontab
In order for a user to create their own crontab and have cron follow its schedule, the user has to belong to the "crontab" group.
sudo /usr/sbin/usermod -a -G crontab demouser
Similarly, to remove a user's access to the crontab you can remove them from the crontab group. Unfortunately removing groups from a user isn't quite as simple, since you have to pass usermod the full list of groups you want the user to be a member of. By omitting the crontab group from the list, the user is removed from that group. Use the "id" command to get a list of a user's current groups, like so:
$ id -nG demouser demouser wheel crontab www-data $ sudo /usr/sbin/usermod -G demouser,wheel,www-data demouser
The "main" crontab
One more note about the crontab is that there's a "main" crontab file at:
This file usually contains the schedules for the periodic cron directories (cron.hourly, cron.weekly, etc.). The only difference between this file and a user's crontab entry is that the crontab file includes the user the command will run as, similar to an entry in /etc/cron.d.
Bonus command: at
One extra command we'll mention is the "at" command. Where cron handles repeating schedules, the "at" command can be used for one-shot schedules. You might need to install the "at" package, since not all distributions include "at" these days.
at 4:30pm August 12
The at command is wonderfully flexible in terms of how you specify the time, so long as the time precedes the date. If it were Monday April 4, the following commands would all schedule a command to run on Tuesday April 5 at 2pm:
at 2pm tomorrow at 2pm +1 day at 14:00 April 5 at 2:00pm Tuesday
Once you've established the time at which the command will run, at will ask for the command (or commands) to be scheduled. Enter the commands you want to run as if you were typing them at the command line. When you're done entering commands, type control-D (that's holding down control and then D) to tell it the input is complete.
To see what jobs have been scheduled with at, run:
You will see a list of job numbers and the commands each job will run. You can delete a pending at job with the "atrm" command, with the job number as the argument.
The at command is handy when you just need to set up a quick one-time scheduled command, like restarting a busy web server in the middle of the night following a configuration change.
You should now have a good grasp of how to schedule tasks in Linux. There are more options available when scheduling with cron (including keywords like "@hourly" and "@reboot"), so if you find that you need even more flexibility it could be worth delving deeper into cron's documentation. There are also cron supplements out there like "anacron" (which is good for scheduling jobs on machines that won't necessarily be running all the time, like workstations). Basically, whatever you want to schedule, you can. At worst it's a matter of working out a weird string of numbers and asterisks to tell cron exactly when you had in mind.
- -- Jered