Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error from Popen only when run from cron

695 views
Skip to first unread message

Larry Martell

unread,
Jan 27, 2018, 10:59:50 AM1/27/18
to
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?

Chris Angelico

unread,
Jan 27, 2018, 11:09:41 AM1/27/18
to
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

Grant Edwards

unread,
Jan 27, 2018, 11:23:10 AM1/27/18
to
On 2018-01-27, Larry Martell <larry....@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?

Try using the complete path of the executable. Cron jobs run with a
pretty limited set of environment variables and may not have the PATH
value you expect.

--
Grant


Dan Stromberg

unread,
Jan 27, 2018, 11:30:50 AM1/27/18
to
If you have your script set $PATH and $HOME, you can test it with:

env - ./my-script

The difference between running a script from the command line, and
running a script from cron, is often environment variables. env -
clears the environment.

On Sat, Jan 27, 2018 at 7:58 AM, Larry Martell <larry....@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?
> --
> https://mail.python.org/mailman/listinfo/python-list

Larry Martell

unread,
Jan 27, 2018, 11:43:05 AM1/27/18
to
On Sat, Jan 27, 2018 at 11:09 AM, Chris Angelico <ros...@gmail.com> wrote:
> On Sun, Jan 28, 2018 at 2:58 AM, Larry Martell <larry....@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.

Thanks! Using the full path fixed the issue.

Wildman

unread,
Jan 27, 2018, 11:52:46 AM1/27/18
to
Cron provides this as $PATH: /usr/bin;/usr/sbin

From within a terminal enter: whereis service

If service is not in Cron's $PATH, that is your problem.
Adding the complete path to 'service' in the script
should fix things.

If service is in Cron's $PATH, I have no further ideas.

--
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!

daniel.ch...@gmail.com

unread,
Apr 17, 2018, 7:12:06 PM4/17/18
to
Larry, I have exactly the same problem. I'd like to run a script and from normal user it works, and from cron doesn't.

In sumarry I was googled to find information how to set two or more env. variables and pass them to subprocess.open. I also try to read $HOME/.profile where usually these env. var. are setting. Have anyone see any example how to do it? Please let me know. Regards, Daniel

Dan Stromberg

unread,
Apr 17, 2018, 9:54:26 PM4/17/18
to
Is service on your $PATH in the cronjob?

What if you set the variables you need in the program, and test it
with "env - myscript" in a login shell, to run myscript with an empty
environment? This tends to make under cron and not under cron more
similar.

HTH
0 new messages