Tuesday, August 7, 2012

Why crontab does not work?

I've try to set my python script run daily use crontab.
If your dont know what is cron, learn it now:

If your crontab is working well now, skip this post. Otherwise, try to figure out why it does not work.

0. Find out whether cron daemon running or not

hvn@squeeze:~/Apps/birth-reminder$ pgrep cron

If you got a number, it is running. If not, start it up
sudo service cron start

1. Make sure your time is set right
If your clock is wrong, then crontab will not run task on time
You should check your hardware time instead of system time.
Hardware time:
sudo hwclock

System time:

2. Test your script.
Eg, if you want to use a python script, it may work perfectly when you run it with
python script_name.py

chmod a+x script_name.py

maybe it not run with full absolute path. Try run it with full path. Eg:
If it not work, fix it with this solution:


3. Check some popular reasons make cronjob not run task
From this list:

4. Read log, mail and see what happened.
Change your cronjob to run every minute to "debug" it

In Ubuntu(12.04)/Debian(6.0), read log file:
hvn@squeeze:~$ sudo tail -f /var/log/syslog
[sudo] password for hvn:
Aug  8 09:42:26 squeeze crontab[2039]: (hvn) BEGIN EDIT (hvn)
Aug  8 09:42:37 squeeze crontab[2039]: (hvn) REPLACE (hvn)
Aug  8 09:42:37 squeeze crontab[2039]: (hvn) END EDIT (hvn)
Aug  8 09:43:01 squeeze /usr/sbin/cron[1530]: (hvn) RELOAD (crontabs/hvn)
Aug  8 09:43:01 squeeze /USR/SBIN/CRON[2073]: (hvn) CMD (~/Apps/birth-reminder/reminder.py)
Aug  8 09:45:02 squeeze crontab[2223]: (hvn) BEGIN EDIT (hvn)
Aug  8 09:45:13 squeeze crontab[2223]: (hvn) REPLACE (hvn)
Aug  8 09:45:13 squeeze crontab[2223]: (hvn) END EDIT (hvn)
Aug  8 09:45:17 squeeze crontab[2227]: (hvn) LIST (hvn)
Aug  8 09:46:01 squeeze /usr/sbin/cron[1530]: (hvn) RELOAD (crontabs/hvn)
Aug  8 09:46:34 squeeze anacron[1456]: Job `cron.daily' started
Aug  8 09:46:34 squeeze anacron[2256]: Updated timestamp for job `cron.daily' to 2012-08-08

when your cronjob run, you will see a log with

Read mail:
Each time cronjob run, it will send an email to your mailbox host on your computer. You may find it in /var/mail/your_name
See what happened in mail content

hvn@squeeze:~$ tail -n 15 /var/mail/hvn
Date: Wed, 08 Aug 2012 09:43:20 +0700
Message-Id: <E1SywEy-0000Xr-T6@squeeze>
From: root@squeeze (Cron Daemon)
To: hvn@squeeze
Subject: Cron <hvn@squeeze> ~/Apps/birth-reminder/reminder.py
Content-Type: text/plain; charset=UTF-8
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/hvn>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=hvn>

Sent to  ['bot.c2k8pro@gmail.com']
Today is  08/08
Tomorrow is Trịnh Thị Thu Hiền's birthday
18 mails:

You may got a log from log said that you have no email agent. So install one MTA (Eg: postfix, exim4...)

crontab, why does crontab not run, crontab not run, crontab doesnt run, why doesn't crontab work

No comments: