Skip to main content

crontab - Scheduler and logging

Crontab - Crontab is scheduler so you can launch scripts hourly/daily/weekly/monthly.

  1. type "sudo crontab -e" (Edit the crontab)
  2. Pick 1. nano unless you like other editors.

image.png

Add the following to the bottom of the crontab to run the script at 10AM Sunday

0 10  0 /scripts/<starchiver script> -options

image.png

CRONTAB LOGGING:

You also want  better crontab logging! this is easy.  Edit this file with nano nano /etc/rsyslog.d/50-default.conf
and remove the # from the line below.  The run sudo systemctl restart rsyslog which restart crontab.  There will be a new log
for your jobs in /var/log/cron.log

 

#  Default rules for rsyslog.
#
#                       For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
cron.*                          /var/log/cron.log
#daemon.*                       -/var/log/daemon.log
kern.*                          -/var/log/kern.log
#lpr.*                          -/var/log/lpr.log
mail.*                          -/var/log/mail.log
#user.*                         -/var/log/user.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
#mail.info                      -/var/log/mail.info
#mail.warn                      -/var/log/mail.warn
mail.err                        /var/log/mail.err

 

CRONTAB LOGGING DETAILS

Now setup cron to give you more logging details.  Run the following command sudo systemctl edit --full cron.  You will want to add -L 15 to the ExecStart line like so

[Unit]
Description=Regular background program processing daemon
Documentation=man:cron(8)
After=remote-fs.target nss-user-lookup.target

[Service]
EnvironmentFile=-/etc/default/cron
ExecStart=/usr/sbin/cron -f -P -L 15 $EXTRA_OPTS
IgnoreSIGPIPE=false
KillMode=process
Restart=on-failure
SyslogFacility=cron

[Install]
WantedBy=multi-user.target

 

Save and run sudo systemctl restart rsyslog again to restart the cron scheduler

The log will now have these details:

  • 1: Start of all cron jobs
  • 2: End of all cron jobs
  • 4: Failed jobs (exit status != 0)
  • 8: Process ID of all cron jobs