Sep 202012
 
Article Server Administration

By default, a cron entry in our Unix/Linux server sends and email with the output generated by the scheduled job.

To gain more control and avoid other issues that could be related to the mail service itself, we can redirect the output to a file, adding a timestamp with the time of execution.

For instance, a typical cron entry to run ‘command_to_run’ every day at 08:05 is:

we can rewrite the entry in this way:

We can see that we have added a ‘date’ command preceding the command to be executed. They both are enclosed in parentheses to force them to be executed inside a subshell.

The redirection “>> /tmp/crontab_output.log” tells the system to write to the file ‘/tmp/crontab_output.log’ any output written by the commands to standard output

The redirection “2>&1” tells the system to redirect to standard output (file descriptor 1) any output written to standard error (file descriptor 2). Therefore, output to standard error will also be written to the file ‘/tmp/crontab_output.log’.

Finally, the trailing  ‘&‘ character tells the system to run the commands in a detached subprocess. This additional measure ensures that, even if the execution of the job takes a long time, if won’t prevent the execution of other cron jobs.

Note: When the ‘date’ command is executed interactively, the time returned is relative to the time zone configured for the user.

However, when adding of editing a cron entry, the UTC time must be used.

Example:

We can see that the first date command returned 16:25 CEST (Central European with daylight Saving Time). But adding the “-u” flag, it returned  14:25 UTC (a two hour difference).

Therefore, the sample cron entry above will be executed daily at 08:05 UTC (10:05 local time).

 Posted by at 6:24 pm

  One Response to “How to fix issues in the execution of cron jobs”

 Leave a Reply

(required)

(required)