Linux scheduler : Cron, At jobs

cronjobsLearn everything about Linux/Unix schedulers i.e. cron and at. Know how to schedule cronjobs and atjobs, their configuration files, log files.


 

Unix or Linux comes with native in-build job scheduler i.e. cron and at. Out of which cron used to schedule tasks to repeat over some period while at used to execute job at a specific time one time.

Cron

Cron enables administrators/users to execute particular script or command at given time of choice repetitively. Its a daemon which runs in background whenever system clock configured time it executes respective script or command. It can be checked if running with ps/service command.


Configurations:

Cron saves commands/scripts and related schedule in file called crontab. Normally crontab can be found in path /var/spool/cron and file with username (root user crontab file can be seen in below example). These are plain text files which can be viewed using cat, more commands and can be edited using text editor.

But, its not advisable to edit crontab file with text editor, you need to use crontab -e username command to edit it so that syntax can be verified before saving. This command opens crontab file in native text editor only.

Cron access can be given user basis. Administrator can enable or disable cron access to particular user. There are two files cron.allow, cron.deny; either one of which will exist on server. These are files with usernames only. No special file format/syntax follows within. If both files are missing then only superuser is allowed to use cron.

If cron.allow exists on server then only users specified in this file are allowed to use cron, rest all are denied. And if it exists and empty then all are denied.

If cron.deny exists then only users specified in it are not allowed to use cron, rest all are allowed. And if it exists and empty then all are allowed.


Syntax:

Lets see syntax for crontab file and commands.

Crontab file has 6 fields separated by space to be filled in. Those are as below :

cron file format

where,

Minute : Timestamp in 24 hts format
Hours : Timestamp in 24 hrs format
Day of month : Date in dd format
Month : Month number in mm format or jan, feb format.
Day of week : Numeric/text day of week. 0 or 7 being Sunday or sun, mon etc.

These fields also supports series of values or multiple values example 1,2,3 or 1-4. When multiple time values defined then event will happens whenever clock hits one of the values.

Default cron definitions i.e. path or shell used to execute commands/scripts in crontabs etc are defined in /etc/crontab file. See example below :

Crontab commands :

We have a crontab command with several options to play around configurations.

  • -u : Specify user
  • -l : to view specified user’s crontab
  • -e : to edit specified user’s crontab
  • -r : to remove specified user’s crontab
  • -i : Interactive removal. Should be used with -r

If new crontab is being set then system will show using empty crontab for user!


Cron logs :

All activities by cron deamon are logged in logfile /var/log/cron. It includes crontab alterations and cron deamon executions. Lets look at file

In above example you can see, crontab alteration are being logged with what actions took place. Those logs are against crontab field where first braces shows user who did alterations and last braces shows which user’s crontab was altered. In last two two you can see cron commands being executed by cron deamon according to schedule hence logged against CROND. This file is very helpful in troubleshooting issues related to cron executions.


at

At enables administrators/users to execute particular script or command at given time of choice only once. It can also termed as one time task scheduling.  Same as crond, daemon for at is atd which runs in background. This can be checked using ps or service command.


Configurations:

at stores submitted jobs in files located at /var/spool/at where file names are system generated and unlike crontabs these files can not be read.

at access also can be given user basis. It also has at.allow and at.deny files and those works same as cron.allow and cron.deny files we seen earlier in this post.


Syntax:

at command should be supplied with the time you prefer to execute command. Once given in proper format, it will present you with at prompt. This prompt takes command inputs which needs to be executed at given time. Once finished entering commands/ scripts one can simple press ctrl+d to exit out of at prompt and save the job. Observe new file is being generated at above mentioned path once you submit the job. at commands takes numerous type of time formats like noon, mignight, now + 2 hours, now + 20 minutes, tomorrow, next monday etc. If you enter wrong format it will return “garbled time” error message.

To view currently queued jobs in at scheduler run atq or at -l command. It shows output with numbering in first column. Second field is about time when execution will happen and last field is username.

To remove particular job from queue atrm command is used. It should be supplied with serial number of the job. In below example we removed job number 2. You can see its vanished from queue. Same can be achieved using at -r command instead of atrm.


at logs:

at daemon is very much regressive in terms of logging. Normally it does not log anything anywhere about its job queue alterations or job executions. Only fatal errors related to daemon are logged in syslog only. Even if we turn debugging on, it logs information which is merely informative to look at.

 

Any thoughts?