1

I am running Raspbian Stretch and have a python 3 script that executes correctly when I am in the relevant directory and input it in the command line as:

sudo python3 Example.py

The shebang #! line is in the script as follows:

 #!/usr/bin/env python3

And made the script executable with:

sudo chmod +x Example.py

I've tried several variation of below within crontab -e to no avail

10 * * * * python3 /home/pi/Desktop/Example.py

6

As is typical with crontab issues. The environment is not the same as your user or even a sudo/root shell , and env python3 may not return anything (env simply searches through

Instead you should explicitly use the full path of python which can be found either as which python3 or which $(env python3) and use that full path in your crontab (typically /usr/bin/python3 but it is also valid to use one installed elsewhere , e.g. a virtualenv directory)

If your script or program relies on being in the same directory, make sure to change directory

cd /path/to/dir && /usr/bin/python3 ./Example.py

  • Note shebangs are ignored when called script is called explicitly as argument to python – crasic Mar 1 at 17:30
  • The script should also cd <relevant directory>. – Ingo Mar 1 at 19:46
  • @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness – crasic Mar 1 at 20:34
  • Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory. – Ingo Mar 1 at 21:28
  • 1
    @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper' – crasic Mar 2 at 6:15
1

Try the following in crontab:

sudo python3 /home/pi/Example.py

You need the full path to Example.py

You don't really need the shebang.

Or sudo crontab -e for the root crontab with:

python3 /home/pi/Example.py

  • 1
    The script should also cd <relevant directory>. – Ingo Mar 1 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.