How to work with Linux Cron and Crontab utilities

This is a very useful and powerful Linux tool, every System Administrator will take full advantage of crontab futures.

Why is Crontab important and useful ?

Well we can create routines and maintenance jobs that will run in the background automatically, following the desired schedule. crontab

Syntax

    bash:crontab [option]

Options

  • [-e] - this option is used when we want to edit the current crontab file, it will open the crontab file into a editor.
  • [-l] - this option is used when we want to list and see the content of the crontab file.
  • [-r] - this command will remove the crontab file.
  • [-u] - this option will point to who's crontab file will be edited, if this option is not used by default the current user crontab file will be used.
  • [-v] - will show the last time the crontab file was edited.

Crontab Format

  • this is an example of a crontab content
The crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

We can also use the  *(wildcard)

- witch is a wildcard(can take all the values contained in the current field is encountered).

How to view or alter the crontab file of another user

For this task you need to be root user. Example
bash:crontab -u "user name" -l
-this way we can list the crontab file of the named user.

Crontab Restrictions

You can execute crontab if your user name appears in the file "/usr/lib/cron/cron.allow".If that file does not exist and your name does not appear in the file "/usr/lib/cron/cron.deny" you can use crontab. We cannot create a job in the crontab file that will run more often then every 60 seconds.

More examples of crontab commands

1- How to view the crontab of another user -you need to have administrator privileges in order to use the -u option.
bash:sudo crontab -u jeff -l
2- Remove/Un-schedule all crontab jobs.
bash:crontab -r
3- Create a job that will run every minute 
* * * * * command or /path/script
 4- Create job that will run every 5 minutes
*/5 * * * * command or /path/script

Even better now that you know how crontab works you can use the tool below to generate you own crontab commands

The corntab generator belongs to crontab-generator.org