Baobab1988
Baobab1988

Reputation: 715

Cron job fails when executing python script with subprocess

My cron runs ok when it's set up like this:

* * * * * usr/local/bin/python3 /Users/username/path/to/my/script1.py
* * * * * bash -c -l "/Users/username//path/to/my/script2.sh"

When I run the above scripts manually in terminal like this, it also works:

usr/local/bin/python3 /Users/username/path/to/my/script1.py
sh /Users/username//path/to/my/script2.sh

However, when I try to run both scripts as a subprocess in a parent.py script scheduled as cron job then it won't work and throw this error:

/bin/sh: usr/local/bin/python3: No such file or directory

My parent.py code:

import subprocess

subprocess.run('usr/local/bin/python3 /Users/username/path/to/my/script1.py && sh /Users/username//path/to/my/script2.sh', shell=True)

however, when I run parent.py manually in terminal like this, it works fine:

usr/local/bin/python3 /Users/username/path/to/my/parent.py

I've tried adding #!/usr/local/bin/python3 to my parent.py but this didn't help.

Could someone help with this? Thanks in advance.

Upvotes: 0

Views: 250

Answers (1)

forgetso
forgetso

Reputation: 2478

You are missing a slash before usr/local/python3!

It should be /usr/local/python3

See this for more details.

Upvotes: 1

Related Questions