Running python script in bash file with php

I am trying to run a simple python script (name.py):

#!/usr/bin/env python 
 name = raw_input('What is your name?\n')
 print 'Hi, %s.' % name

      

with the following bash script (Helloworld.sh):

 #!/bin/bash

python name.py

      

via the following php

<!DOCTYPE html>
<html>

<body>

<?php
if (isset($_POST['Submit1'])) {

 echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}

  ?>
<form action="myfilename.php" method="post">
<p><Input Type = "Submit" Name ="Submit1" Value = "Save Parameters">

</form>
</p> 

</body>
</html>

      

Log error:

python: unable to open file 'name.py': [Errno 2] No such file or directory

The bash file works fine with the terminal. What should I do?

+3


source to share


2 answers


You can change your shell script like this:

#!/bin/bash
BASEDIR=`dirname "${0}"`
cd "$BASEDIR"
python name.py

      



always run in the directory containing the script. Or, if the .py name is in a different directory, then change the command accordingly cd

.

+1


source


try adding a line before executing the python script

cd "$(dirname "$0")"

      



in your bash file?

+2


source







All Articles