Python Forum

Full Version: subprocess Popen not working via crontab
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey All,

Okay , I'm gonna admit this issue has me completely stumped and i'm at my wits end.

Below is the code that has been troubling me and i have tried all the things i know or could have googled.

session = Popen(['dgmgrl','username/password@dbname','stop observer', '-logfile', '/u01/app/oracle/broker/fsfo_mydg.log'],stdout=PIPE, stderr=PIPE, cwd=r'/u01/app/oracle/product/11.2.0/db1/bin/')
output,error = session.communicate()
if session.returncode != 0:
    raise RuntimeError("broker command failed with %s stdout %r stderr %r" % (session.returncode, output, error))
else:
    print >> file, "The stdout is :\n %s" % output
    file.flush()
So while trying to execute above directly on the terminal , it executes perfectly , But it simply does not runs and throws below error when scheduled to run from crontab.

Error:
OSError >  2 OSError >  No such file or directory OSError >  None
or 

Error:
RuntimeError >  broker command failed with -11 stdout '' stderr '' Successfully sent email at 2017-03-15 09:32:10.226847
and i have tried all combinations of command formatting.

Please help.

Regards,
Nitin
I assume things in crontab are executed with a very specific user and environment, and the PATH can be different from what you have in a plain user session. So for a start I would give the full path to the executable, but this could not be sufficient.

As an experiment, write a Python script that reports the executing user id, the working directory and dumps the environment variables and have it execute from crontab.
(Mar-15-2017, 09:37 PM)Ofnuts Wrote: [ -> ]I assume things in crontab are executed with a very specific user and environment, and the PATH can be different from what you have in a plain user session. So for a start I would give the full path to the executable, but this could not be sufficient.

As an experiment, write a Python script that reports the executing user id, the working directory and dumps the environment variables and have it execute from crontab.

Thanks for reply Ofnust,

I was able to solve it yesterday but due to deadlines could not post an update.

You are right , the issue was because of an environment variable needed by oracle utilities.

below is the code which solved it.

session = Popen([r'/u01/app/oracle/product/11.2.0/db1/bin/dgmgrl','username/password@dbname','stop observer', '-logfile', '/u01/app/oracle/broker/fsfo_mydg.log'],stdout=PIPE, stderr=PIPE, env=dict(os.environ, ORACLE_HOME="/u01/app/oracle/product/11.2.0/db1"))
Regards,
Nitin
Thanks for coming back and letting us know what the problem was :)

I've had the same issue before (...more than once). Cron doesn't have any environment values set, so you need to use absolute paths, and the user and user group are different, so if you do any file reading/writing, the files you touch need pretty promiscuous permissions.
(Mar-17-2017, 07:27 PM)nilamo Wrote: [ -> ]Thanks for coming back and letting us know what the problem was :)

I've had the same issue before (...more than once).  Cron doesn't have any environment values set, so you need to use absolute paths, and the user and user group are different, so if you do any file reading/writing, the files you touch need pretty promiscuous permissions.


The thing is i couldn't understand what " -11 " exit code represents , Yhg1s from python irc forum replied back that -11 is exit code for segmentation fault.

Then it became clear what the issue is. For oracle binaries to work ORALCE_HOME needs to be set , if it is not set i have seen segmentation fault issue.

Regards,
Nitin
...oracle segfaults if environment variables aren't set? Yeah, THAT'S safe lol
Despite its prices, corporate software is full of silly/innervating bugs like this, because the people who make the purchase decisions aren't those who use it so there is little incentive to fix them. IT operations people put up with a lot of crap.
Indeed. When I was fresh from university I had opportunity to watch a tender for some simulation software (odd financial product).  Original request was for simulation, on way through higher management they added support for planning and accounting (with same weights). And at the end they bought only one offered software that couldn't simulate at all, but had nice planning and accounting. It was something like million $ for four year licence and another million for four year support and it was almost never used...
My day job is basically building reports that are 100% top priority that need to be done right now, that people are surprised they didn't already have. They use it for maybe a week, and then look at it maybe once a month afterwards.

I get the same handful of requests every threeish months, so at least now I can just give them a url to go to and save myself some time.