1. Introduction

Sometimes, we might need to log the cron job output on stdout or any other log file for monitoring and maintenance purposes. Fortunately, we have several methods that we can use to log the cron job output. In this tutorial, we’re going to take discuss approaches that we can use to log the cron job output.

2. Cron Job Output

Whenever we run the cron job, it generates logs, which are quite useful for troubleshooting. The output of a cron job is stored directly at the /var/spool/cron/crontabs location. However, we can control this output and send it to stdout. Alternatively, we can also send this output via email or system logs.

This is essential for Linux administrators as it helps them in organizing all the cron job outputs in one place. Therefore, it’s easier to go through all of them at once instead of switching between directories.

By default, the cron job output is stored at /var/log/syslog on Ubuntu and Debian systems and in /var/log/cron on CentOS. The cron daemon runs the cron jobs listed in crontabs depending on the timings of the cron job. If we want to view the cron log output, we can view these files by applying the filter for “cron” using the grep command:

$ cat /var/log/syslog | grep cron

This will grab any cron job log saved in the syslog file.

3. Redirect Cron Job Output to stdout

We can redirect the cron job output to stdout using the redirection operator in the cron job:

* * * * * /path/to/command > /dev/stdout 2>&1

For instance, let’s say we have a cron job that runs every 10 minutes and prints “Hello World”. We can redirect this cron job output to stdout. For this step, we’ll first open the crontab file editor:

$ crontab -e

Next, we’ll redirect the output to stdout:

*/10 * * * * echo "Hello, world!" > /dev/stdout 2>&1

This will redirect the output as well as the error to the standard output. Alternatively, we can do the same with any Bash script file. For example, if the cron job runs /home/user/Documents/myScript.sh every 10 minutes, then the cron command will use the file name in place of the echo command:

*/10 * * * * /home/user/Documents/myScript.sh > /dev/stdout 2>&1

Using the > instead of >> is recommended with stdout, as >> might cause formatting problems. However, we can also use the append operator (>>) instead of the overwrite operator (>) operator if we are sending the logs to a separate file. This will prevent the file from being overwritten.

We’ll now redirect the output to a file along with the timestamp to keep the file organized:

*/10 * * * * /bin/bash -c "echo $(date) >> /path/to/logfile.log && /home/user/Documents/myScript.sh >> /path/to/logfile.log 2>&1"

Here, we’ve used the /bin/bash -c command. This allows us to run multiple commands within the cron job command. The $(date) will insert the current date and time into the log file. Also, we’ll have to replace the /path/to/logfile.log with the actual log file location.

4. Send Cron Job Output to syslog

In addition to directing the output to stdout, we can also direct the output of a cron job to the syslog. First, let’s open the crontab editor:

$ crontab -e

Now, to redirect the output of /home/user/Documents/myScript.sh to syslog, we’ll add /usr/bin/logger instead of /dev/stdout:

*/10 * * * * /home/user/myscript.sh 2>&1 | /usr/bin/logger -t CRONOUTPUT

The */10 * * * * is a cron job that will run every 10 minutes. Next, we have the path to the shell script that will be executed in the cron job. 2>&1 will forward the standard output and error to the command written after the pipe (|) operator.

The standard output and error will be piped to /usr/bin/logger with the title CRONOUTPUT. The option -t represents the tag of the output. In this example, we’ve passed the CRONOUTPUT tag with our cron job. Hence, our cron job will be saved with the title CRONOUTPUT in the logger file.

Alternatively, we can also write the command without the complete logger path:

*/10 * * * * /home/user/myscript.sh 2>&1 | logger -t CRONOUTPUT

Now, to view the output of the cron job saved in the system log, we’ll use the cat command in combination with the grep command:

$ cat /var/log/syslog | grep CRONOUTPUT

This lists all the available cron job outputs.

5. Redirect Cron Job Logs to a Separate File

We can also redirect the cron job outputs to a separate log file, created especially for cron logs. To do this, we’ll open the default configuration file in the nano editor:

$ nano /etc/rsyslog.d/50-default.conf

Here, we’ve used the rsyslog file. rsyslog is an implementation of the syslog protocol that is the replacement for the original syslogd daemon. It’s a simple and reliable logging solution. However, if we want advanced filtering and routing capabilities, syslog-ng is a better option.

syslog-ng is a modular and flexible implementation of the syslogd protocol. It not only offers message filtering, parsing, and routing but also allows us to customize and configure logging behavior.

Once the file is open, we’ll uncomment the following line of code:

#cron.* /var/log/cron.log

After uncommenting, it’ll look something like:

cron.* /var/log/cron.log

Now, we’ll press Ctrl + S to save and Ctrl + X to exit the nano editor. Next, we’ll create the cron.log file:

$ sudo touch /var/log/cron.log

After that, we’ll assign the write permission to the syslog user:

$ sudo chmod 640 /var/log/cron.log
$ sudo chown syslog:adm /var/log/cron.log

Lastly, we’ll restart the rsyslog service:

$ sudo systemctl restart rsyslog.service

Now, once the cron jobs run, the output will be logged in this cron.log file.

6. Cron Job Best Practices

Since cron jobs are a part of most Linux users’ lives, we’ve gathered a few best practices for working with cron jobs:

  • Always specify a full path to the command or the script.
  • Redirect the output to a separate log file to maintain the record.
  • Ensure that cron jobs do not overlap or conflict with each other.
  • Use the descriptive comments in the crontab file so that other users can understand it, too.
  • Test cron jobs before deploying them to production.

7. Conclusion

In this article, we discussed how to redirect the output of a cron job. First, we’ve covered how we can redirect the output to stdout. After that, we discussed how to send the output to syslog or a separate log file. Lastly, we’ve pointed out some of the best practices when handling cron jobs.

As we’ve discussed in the best practices, maintaining a separate file for the cron job output is efficient for maintaining and monitoring the cron logs. Hence, we should redirect the cron job outputs to an independent log file.