1

I've got a python file called color.py that if I run using python color.py will work on a file. I can run it okay from the terminal but it doesn't get called from crontab. I've used chmod +x color.py to try and make it executable.

The py file does start with

#!/usr/bin/env python

and the cron command is

*/1 * * * * /root/images/color.py
eguaio
  • 3,754
  • 1
  • 24
  • 38
Samuel M.
  • 91
  • 1
  • 3
  • 12

1 Answers1

1

First check if following command works by running as user root (su or sudo):

/usr/bin/python /root/images/color.py

If that works, then edit crontab to:

*/1 * * * * /usr/bin/python /root/images/color.py

How do you check if the cron job succeeds or not?

anneb
  • 5,510
  • 2
  • 26
  • 26
  • the command works and it deletes a file(s) that's how I know the task's done – Samuel M. Jul 14 '17 at 22:48
  • do you reference the file to be deleted by its full path? Not delete 'myfile' but something like delete '/root/images/myfile' ? – anneb Jul 14 '17 at 22:48
  • using os.remove(filename) @anneb – Samuel M. Jul 14 '17 at 22:51
  • is ' filename' full path or not? If it is 'myfile' it will only work if you explicitly change the working dir to the directory containing the file, if it is '/root/images/myfile' then that should be ok – anneb Jul 14 '17 at 22:53
  • I've edited to this been more than a minute but the file still is there, I know if I run python color.py it's going to colour the image make a new file and delete the old one. – Samuel M. Jul 14 '17 at 22:58
  • the cron job normally runs in /root, to check manually if your python program is ok: first 'cd /root' then '/usr/bin/python /root/images/color.py' Does that work? – anneb Jul 14 '17 at 23:00
  • root@color:~# pwd /root root@color:~# /usr/bin/python /root/images/color.py root@color:~# – Samuel M. Jul 15 '17 at 06:51
  • It works but doesn't work on call, don't know what the issue might be. – Samuel M. Jul 15 '17 at 06:53
  • put `*/1 * * * * /usr/bin/python /root/images/color.py >> /var/log/color.log 2>&1` in your crontab and inspect /var/log/color.log for error messages – anneb Jul 15 '17 at 09:49
  • What is your username? Use "whoami", is it root or something else? When you test the python script, what is your directory? Use "pwd" to find out. The code in file color.py script does NOT use full path names, so it can only work if running in the correct directory. If the correct directory is the directory containing color.py, then add the following to python script: `abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname)` – anneb Jul 16 '17 at 09:53
  • I've found out there's a script that crashes when it's running from cronjob only. – Samuel M. Jul 17 '17 at 05:58