Python call to bash script returns empty string

Hi all,

I need help figuring out why my script won't work when envoked from web interface. First off, I'm fairly new to Linux and Shell scripting so, my apologies for being ignorant!

So I have a python script that I envoke through a web interface. This script envokes my shell script and then the shell script returns values to my python script using subprocess. this generally works when tested from the command line but fails or returns empty string when called from the interface. Please see my python script code below and then my shell script below it.

Python code: function envokes shell script passing args to it and returns results back to interface.

#! /usr/bin/env python
 
import subprocess
import sys
 
def PDFGenerator(args=''):
    if args:
       proc = subprocess.Popen(['bash PDFGenerator.sh '+str(args)], stdout=subprocess.PIPE, shell = True)
       #return str(proc.communicate())
       print proc.stdout.read()

============================================

#Shell script: calls a java file which returns value into args variable which is then echoed back.
#I've even tried putting in just echo "hello world" and returns empty to interface.

args=$(java Test $@ | tail -1)
echo $args

java is probably not in your PATH. You also need to worry about CLASSPATH and such. A web interface is not going to give you the same environment variables a login shell does.

1 Like

Even if I remove the java call and just replace it for some hard-coded value in my bash script, my python script still returns empty string.

for example I commented the java call line and just left this line as such and i still get ('', None) for the result?

 
echo $@

---------- Post updated 05-15-12 at 08:29 AM ---------- Previous update was 05-14-12 at 02:01 PM ----------

Anybody knows what's going on here? Thanks!

---------- Post updated at 08:30 AM ---------- Previous update was at 08:29 AM ----------

Can someone help with this?

could be a PATH issue try fully pathing your script:

proc = subprocess.Popen(['/usr/bin/bash /home/arod291/scripts/PDFGenerator.sh '+str(args)], stdout=subprocess.PIPE, shell = True)

Check your system logs, selinux may be blocking python/apache from running bash scripts.

1 Like

I tried your code but although it works from the command line, it won't work from the browser. I also checked all these logs and didn't see anything pointing to blocking it:

  • /var/log/message
  • /var/log/auth.log
  • /var/log/httpd
  • /var/log/lighttpd
  • /var/log/boot.log
  • /var/log/secure

which log file exactly would i need to look at?

BTW the bash file resides in the same directory as the python file and this directory has all rights issued for writing, reading, executing, etc. whenever i run my python script from the putty command line, it finds the bash file with no problems and executes it. the problem is mainly when running my python script from the browser since it is attached to a plone web application through ZMI.

Appreciate all the help!

did you try to delete the space between #! and /usr/bin/env python ??

#! /usr/bin/env python
 
import subprocess
import sys
 
def PDFGenerator(args=''):
    if args:
       proc = subprocess.Popen(['bash PDFGenerator.sh '+str(args)], stdout=subprocess.PIPE, shell = True)
       #return str(proc.communicate())
       print proc.stdout.read()

---------- Post updated at 16:47 ---------- Previous update was at 16:44 ----------

also try to put below in your code (under your code):


if __name__ == "__main__":
    main()

I removed the spacing and included that code below my python function but it still won't work.

just notice something , where is the call of the function "PDFGenerator" ?

As it turned out, it was a path issue. I was executing my bash file from the same location as the python script and thought it should find it that way until someone with more expertise on the web application we're running told me that was not the case because once the web app loads the python script into memory, when the script executes it doesn't recognize the path location is in so it cannot find the bash script file.

Thank you all who responded!