Debugging cron jobs

  • Open /etc/rsyslog.conf (RHEL 7.x) and check for following line:
# Log cron stuff
cron.*                                                  /var/log/cron
  • The path in previous step gives the filename to which cronjob runs will be logged. Open that file. Whenever a cronjob is executed, an entry is logged into this file. Example below:
Aug 30 01:00:06 azrxxxnval0001 CROND[30300]: (siddjain) CMD (/home/siddjain/cleanup.sh)

If you don’t see an entry, it means your job is not getting scheduled. If this is the problem, check if the cron service is even running.

  • The previous file tells if your job was executed or not, but does not tell if it ran into some error while executing. To find that out look in a file /var/mail/<username>. Example below:
/bin/sh: /home/siddjain/cleanup.sh: Permission denied
  • This tells us the job was executed but ran into an error. To check for file permissions use the namei utility:
$ namei -om /home/siddjain/cleanup.sh
f: /home/siddjain/cleanup.sh
 dr-xr-xr-x root    root   /
 drwxr-xr-x root    root   home
 drwx------ siddjain zusers siddjain
 -rw-r--r-- siddjain zusers cleanup.sh

  • By now its clear what the problem is:
$ chmod +x /home/siddjain/cleanup.sh

Cron jobs run under the environment of the user.

Checking if cron service is running

Sometimes the problem is that cron service is not running. You can check it as follows (on Red Hat 7.x):

#-> service crond status
Redirecting to /bin/systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

To start the service run:

#-> service crond start
Redirecting to /bin/systemctl start crond.service

Check the status again:

#-> service crond status
Redirecting to /bin/systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-09-08 13:29:43 EDT; 4s ago
 Main PID: 4781 (crond)
    Tasks: 1
   Memory: 732.0K
   CGroup: /system.slice/crond.service
           └─4781 /usr/sbin/crond -n

Sep 08 13:29:43 azrxxxnval0001 systemd[1]: Started Command Scheduler.
Sep 08 13:29:43 azrxxxnval0001 crond[4781]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 53% if used.)
Sep 08 13:29:43 azrxxxnval0001 crond[4781]: (CRON) INFO (running with inotify support)

Crontab Commands

crontab -l

List cronjobs of current user

crontab -e

Edit the current user’s cronjobs

References

This entry was posted in Software and tagged , . Bookmark the permalink.

Leave a comment