error from Popen only when run from cron

Chris Angelico rosuav at gmail.com
Sat Jan 27 11:09:08 EST 2018


On Sun, Jan 28, 2018 at 2:58 AM, Larry Martell <larry.martell at gmail.com> wrote:
> I have a script that does this:
>
> subprocess.Popen(['service', 'some_service', 'status'],
> stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>
> When I run it from the command line it works fine. When I run it from
> cron I get:
>
>     subprocess.Popen(['service', 'some_service', 'status'],
> stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>   File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
>     errread, errwrite)
>   File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
>     raise child_exception
> OSError: [Errno 2] No such file or directory
>
> Anyone have any clue as to what file it's complaining about? Or how I
> can debug this further?

Looks like you're trying to invoke a process without a full path? It
could be because cron jobs execute in a restricted environment. Two
things to try:

1) Dump out os.environ to a file somewhere (maybe in /tmp if you don't
have write permission). See if $PATH is set to some really short
value, or at least to something different from what you see when you
run it outside of cron.

2) Replace the word "service" with whatever you get from running
"which service" on your system. On mine, it's "/usr/sbin/service". See
what that does.

Might not solve your problem, but it's worth a try.

ChrisA



More information about the Python-list mailing list