Gavelock Gavelock - 4 months ago 35
Python Question

Run library module as a script by cron (python -m)

I created python library. In order to avoid install of executable python scripts I allowed library modules to work as executables. It is possible to run module as script with -m opion (python -m).
Manually everything works:

$ python -m Library.Core.Runner runFirst


But when I try to run this with cron it does not work:

*/5 * * * * /usr/bin/python -m Library.Core.Runner runFirst >> /var/log/MyProject/runFirst.log 2>&1


Output from /var/log/cron:

Jul 26 18:25:01 myhostname crond[23735]: (/usr/bin/python) ERROR (getpwnam() failed)


How can I fix it?

Environment: CentOS 7, Python 2.7.5

Answer

Add user name to let cron run the task on behalf of him:

*/5 * * * * <username> /usr/bin/python -m Library.Core.Runner runFirst >> /var/log/MyProject/runFirst.log 2>&1
*/5 * * * * igor /usr/bin/python -m Library.Core.Runner runFirst >> /var/log/MyProject/runFirst.log 2>&1