Multi-user task scheduling with crontab
The two previous articles described how to schedule jobs with cron by using basic cron directories and cron.d. One more approach that you can take when scheduling jobs with cron is to use the crontab. The crontab is the oldest and most standard approach to scheduling with cron, so you'll occasionally run into a software package that installs a crontab entry. Using crontab can also be a convenient way to allow 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 that 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 learn how cron.d schedules tasks.
Typically you access the crontab 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 no crontab is set up for that user. For information on adding a crontab user, see Who can use crontab.
If you do see a crontab listing, you'll notice that each line looks like the format used for files in /etc/cron.d.
14 10 * * * /usr/local/bin/anotherprogram --option
The main difference is that crontab entries have no “user” field, because they are set to run as the user that owns that crontab. So the first five entries in the line are 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 option launches your default editor and opens a file that is blank or starts with a commented-out line (a line starting with the "#" character) that lists the order of schedule entries.
Make your changes either by editing existing lines or adding new ones, and then save the file. The file is written to the crontab.
Working with other users' crontabs
As root, or with sudo, you can list or edit other users' crontab entries. To do this, use the "-u" option, followed it with username, and then specify the option that tells crontab what you want to do. For example, the following command would list the crontab entries for the user demouser.
sudo crontab -u demouser -l
The main crontab
A "main" crontab file exists in the following directory:
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.
Carry on the conversation in the Rackspace Community.
© 2015 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
See license specifics and DISCLAIMER