Python script not executed from Cron


I'm trying to run a Python script from cron. Manually functional without problems. I have already tried all the solutions that can be found on the web. The crontab file is as follows:

* * * * * /usr/bin/python /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/ | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/ >> home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1

Crontab , Observations:

  • The 'PATH' obtained from the echo $PATH command is already included.
  • event_management is executable using the sudo chmod a+x <file_name> command.

The log.log file does not show anything weird. The system log file /var/log/syslog contains the following:

Feb 27 15:11:08 raspberrypi cron[21814]: sendmail: Cannot open :25
Feb 27 15:12:01 raspberrypi rsyslogd-2007: action 'action 17' suspended, next retry is Mon Feb 27 15:13:31 2017 [try ]
Feb 27 15:12:01 raspberrypi CRON[22209]: (pi) CMD (/usr/bin/python /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/ | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/ >> home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1)
Feb 27 15:12:09 raspberrypi sSMTP[22212]: Unable to set UsesSTARTTILS=""
Feb 27 15:12:09 raspberrypi sSMTP[22212]: Unable to locate
Feb 27 15:12:09 raspberrypi cron[21814]: sendmail: Cannot open :25
Feb 27 15:12:09 raspberrypi sSMTP[22212]: Cannot open :25
Feb 27 15:12:09 raspberrypi CRON[22205]: (pi) MAIL (mailed 178 bytes of output but got status 0x0001 from MTA#012)

It can be seen that what fails is the crontab line referring to the Python script. I am not an expert in Linux but seeing these logs I have the impression that the problem is something related to the service sSMTP . The same error log is repeated periodically after each call to the Python script by Cron.

Any help is welcome.

asked by BSP 27.02.2017 в 16:03

2 answers


After trying several things and reading the comments of other users, I found the problem. It is a combination of two errors that are not directly related, but together they make the debug complicated.

First problem:

The file log.log contains entries of three cronjobs different. Therefore I did not realize that I had used the chmod command wrongly in the Python script event_management , since there were multiple messages from different sources. The same happens when looking at the file /var/log/syslog . Cron is not the only one who writes there. Therefore it can be confusing to read it to debug.

Conclusion: A cronjob , a file log .

Second problem:

I have two Python distributions installed. When I execute the script manually, one is used, and Cron uses another. I realized this error by correcting the first problem by creating several log files, one for each cronjob. The cronjob running the Python script used a Python distribution that did not have the necessary modules installed. Therefore, it failed to read the import... . To check my Python distribution I used:

which Python

Conclusion: A single distribution, and you avoid problems.

answered by 01.03.2017 / 17:27

I think the bar / behind home / is missing so that it is an absolute path.

It is currently like this:

....../ >> home/pi/PAD-S100/......

and I think it should be like this

....../ >> /home/pi/PAD-S100/......
answered by 01.03.2017 в 15:22