Python call to bash script returns empty string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python call to bash script returns empty string
# 1  
Old 05-14-2012
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.

Code:
#! /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.

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


Last edited by Corona688; 05-14-2012 at 11:43 AM..
# 2  
Old 05-14-2012
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-15-2012
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?

Code:
 
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?

Last edited by arod291; 05-15-2012 at 09:28 AM..
# 4  
Old 05-15-2012
could be a PATH issue try fully pathing your script:

Code:
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.
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 05-17-2012
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!

Last edited by arod291; 05-17-2012 at 10:24 AM..
# 6  
Old 05-17-2012
did you try to delete the space between #! and /usr/bin/env python ??



Code:
#! /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):

Code:
if __name__ == "__main__":
    main()

# 7  
Old 05-17-2012
I removed the spacing and included that code below my python function but it still won't work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use scl enable python command call with in bash shell script?

I have a bash shell script, within it i am using the below two commands . its failing right on scl enable command itself. if i run it by itself without a shell, it works fine. by default it is using pythin version 2.6 something. i want to use 3.4 version for that reason with in the shell... (3 Replies)
Discussion started by: cplusplus1
3 Replies

2. Shell Programming and Scripting

Beginner here, how to call a bash-script from python properly?

Hi everyone, i have the following script.sh: foo='lsusb | grep Webcam | cut -c16-18' sudo /home/user/public/usbreset /dev/bus/usb/001/$foo when i try to call this script from python using subprocess.call("script.sh", shell=True) it seems that only 'sudo /home/user/public/usbreset' is being... (6 Replies)
Discussion started by: hilfemir
6 Replies

3. UNIX for Advanced & Expert Users

Calling expect from shell script which inturn call python

Hi Team, I have to execute a task from my local machine, where i keep my .expect,.sh, .bash and .python scripts .Task are coded in the script and has to be executed at remote machine. for that i used following task ..... SCRIPT 1: cat shell_check.sh read value if then expect... (3 Replies)
Discussion started by: Sivarajan N
3 Replies

4. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

5. Shell Programming and Scripting

[Solved] Unable to call a python script from bash

Hi, I am trying to run a python script embedded in bash script. But is throwing me an error. Please help. Script: #!/bin/bash nohup /usr/bin/python /opt/web/http.py & Error: /usr/bin/python: can't open file '/opt/web/http.py': No such file or directory Please help me on this. (6 Replies)
Discussion started by: maddy26615
6 Replies

6. Shell Programming and Scripting

Shell Variable in Curly Brackets Returns Empty Value

Hello Team, I have a script which will grep for a time from a file. I have following code to grep for a time in a file. node=`hostname` current_date=`date` file11=weblogic.log next_date=`date '+%b %e, %Y'` next_date_time11=`grep -i "${#next_date}" ${file11}| tail -1 | awk... (3 Replies)
Discussion started by: coolguyamy
3 Replies

7. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

8. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 Replies

9. Shell Programming and Scripting

how to call a bash script using perl

Hi I m new to perl. I m trying to write a perl script that calls a bash script; does anyone have a script already that they can provide or help me out? Thanks a lot. (2 Replies)
Discussion started by: adnan786
2 Replies

10. Shell Programming and Scripting

Perl cgi script to call bash script?

Novice to perl here. I have created a simple web page in perl, with only one submit button. I would like to execute a bash script on the same server when this button is clicked on. Is this possible in perl? I have spent a few days researching this and am unable to find any useful information.... (0 Replies)
Discussion started by: pleonard
0 Replies
Login or Register to Ask a Question