I put a cronjob in /etc/cron.{hourly,daily,weekly,monthly} and it does not run and how can I troubleshoot it?


#1

Originally published at: https://www.cyberciti.biz/cloud-computing/why-is-my-linux-unix-crontab-job-not-working/



Recently I created a simple shell script called backup.sh in /root/scripts directory to just backup MySQL database and dumped it to /nfs/mysql/ directory. I put a file (more like used the ln command to create a soft link ) in /etc/cron.hourly/ and it doesn’t run. There was no error in systemd log or cron log. Why is my cron job was not working, and here is how I troubleshoot it.


#2

Hi Vivek,

i have created a test cron viz. */2 * * * * galculator >/dev/null 2>&1

even tried with */2 * * * * /usr/bin/galculator >/dev/null 2>&1

but no go

when i do sudo grep -i cron /var/log/syslog
it says it is running as below

Feb 9 08:17:01 a CRON[24505]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Feb 9 08:18:01 a CRON[25224]: (a) CMD (/usr/bin/ galculator >/dev/null 2>&1)
Feb 9 08:18:39 a crontab[25555]: (a) BEGIN EDIT (a)
Feb 9 08:19:22 a crontab[25555]: (a) REPLACE (a)
Feb 9 08:19:22 a crontab[25555]: (a) END EDIT (a)
Feb 9 08:20:01 a cron[540]: (a) RELOAD (crontabs/a)
Feb 9 08:20:01 a CRON[26217]: (smmsp) CMD (test -x /etc/init.d/sendmail && test -x /usr/share/sendmail/sendmail && test -x /usr/lib/sm.bin/sendmail && /usr/share/sendmail/sendmail cron-msp)
Feb 9 08:20:01 a CRON[26218]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:22:01 a CRON[27446]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:24:01 a CRON[28594]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:26:01 a CRON[29828]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:28:01 a CRON[30907]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:29:59 a systemd[1]: Stopped Trigger anacron every hour.
Feb 9 08:30:20 a cron[537]: (CRON) INFO (pidfile fd = 3)
Feb 9 08:30:20 a cron[537]: (CRON) INFO (Running @reboot jobs)
Feb 9 08:30:21 a systemd[1]: anacron.timer: Adding 4min 52.877209s random time.
Feb 9 08:30:21 a systemd[1]: Started Trigger anacron every hour.
Feb 9 08:30:21 a systemd[1]: Started Run anacron jobs.
Feb 9 08:30:21 a systemd[1]: anacron.timer: Adding 1min 2.905887s random time.
Feb 9 08:30:21 a systemd[1]: anacron.timer: Adding 2min 36.752671s random time.
Feb 9 08:30:21 a anacron[557]: Anacron 2.3 started on 2019-02-09
Feb 9 08:30:21 a anacron[557]: Normal exit (0 jobs run)
Feb 9 08:30:21 a systemd[1]: anacron.timer: Adding 3min 33.716278s random time.
Feb 9 08:32:01 a CRON[4421]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:34:01 a CRON[6570]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:36:01 a CRON[8726]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 9 08:38:01 a CRON[10785]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)

ouput of systemctl status cron

● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: ena
Active: active (running) since Sat 2019-02-09 08:30:20 AEDT; 11min ago
Docs: man:cron(8)
Main PID: 537 (cron)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/cron.service
└─537 /usr/sbin/cron -f

Feb 09 08:36:01 a CRON[8725]: pam_unix(cron:session): session opened for user a
Feb 09 08:36:01 a CRON[8726]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 09 08:38:01 a CRON[10784]: pam_unix(cron:session): session opened for user a
Feb 09 08:38:01 a CRON[10785]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 09 08:40:01 a CRON[12723]: pam_unix(cron:session): session opened for user s
Feb 09 08:40:01 a CRON[12724]: pam_unix(cron:session): session opened for user a
Feb 09 08:40:01 a CRON[12725]: (a) CMD (/usr/bin/galculator >/dev/null 2>&1)
Feb 09 08:40:01 a CRON[12726]: (smmsp) CMD (test -x /etc/init.d/sendmail && test
Feb 09 08:40:01 a sm-msp-queue[12745]: My unqualified host name (a) unknown; sle
Feb 09 08:41:01 a sm-msp-queue[12745]: unable to qualify my own domain name (a)

appreciate help


#3

considering this is a help request should I create new topic on your site? or continue here? What do u prefer?


#4

Hey,

The galculator is a GTK 2 / GTK 3 algebraic and RPN calculator. You simply can’t start galculator or any other GUI apps. The following post should give you some hint on how to start GUI app from cron:

IN your case it should:

*/2 * * * * DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus / /usr/bin/galculator >/dev/null 2>&1

Replace 1000 with actual numeric user id.


#5

Hey,

Thanks for the reply

I managed to get it going with a systemd service and timer. I have set the timer to run every hour which in turn runs my script

the galc was just to test if cron works
i tried even replacing the command with a script but no go

the logs showed the cron ran but I could not see the script doing the same

Anyway for now my issue is solved

many thanks.