Android Question How to run scripts .py and .sh

AlpVir

Well-Known Member
Licensed User
Longtime User
How to run pyton script (*.py) and "shell bash" (*.sh) script that reside on a web server Apache ?
These scripts do not return output but interact with the hardware of the server (that has an address like name.no-ip.com or name.dlinkddns.com or name.ddns.com).
Thank you in advance
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
I can throw a php script but I would do it with python script and especially sh.
This will launch it and I wrote "Now snap a photo done"
B4X:
<? php
Print "Now snap a photo";
echo shell_exec ('unafoto.sh');
echo "done";
  ?>

but this does not run
B4X:
#! / bin / bash
# unafoto.sh
gphoto2 --set-config imagesize = 0 = 2 imagequality --set-config --set-config zoom = $ 1 = 1 iso-config --set-config --set PhotoEffect = 1 \
--set-config shootingmode = 0 --set-config --set-config whitebalance = 2 = 2 meteringmode --capture-image-and-download --force-overwrite \
--filename unafoto.jpg

exit 0

Perhaps because there are no write permissions of JPG file ?
Everything is in /var/www/html
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
All scripts (python of that sh) are working properly.

And for .sh scripts you will need to use web services (a gateway between the web request and the server's interface). You could create your own in the language you prefer (for example in PHP you would use the shell_exec command).
These are issues on which no expert and I are completely new.
What I did
B4X:
<? php
Print "Now snap a photo";
echo shell_exec ('unafoto.sh');
echo "done";
  ?>
not a web services in PHP ?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
not a web services in PHP ?
Yes it is and you have done it like a master :)
Now we have to find why it is not executed :
- have you found if the script was executed ?
- is the server's user allowed to access all the directories used ?
- etc...

sorry it is not so easy as they could be many reasons

PS : of course no space between question mark and PHP
B4X:
<?php

?>
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
The space in the PHP code is due to copy / paste.
Thanks for the informations. So should I go into that "PHP webservice" looking maybe some examples
I have the impression that there are problems in the Apache configuration.
Surely if I had to hand a small example I could make the adjustments ...
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
This one could allow you to check if PHP is getting data from the shell. Call it from your server with a browser and it should print the server's time and time

B4X:
<?php
$mycmd = shell_exec('date');
echo shell_exec ($mycmd);
?>

If it works, you could put the command in a .sh file and call that file from the PHP script
Here are more information (Googled) :
See the answer with the path of the .sh script : http://stackoverflow.com/questions/7397672/how-to-run-a-sh-file-from-php
Permissions denied : http://superuser.com/questions/455935/php-script-cant-run-bash-script-sh-permission-denied
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
From a quick test
B4X:
job2.PostString ("http://nomesito.ddns.net/lemon.php", "")
the code in the sub Jobdone it does not return anything.
instead
B4X:
job2.PostString ("http://nomesito.ddns.net/unafoto.php", "")
he returned "Now snap a photo done".
You gave me some subject of further consideration. I will do it tomorrow because now, in Italy, it is time for dinner.:)
Tank you !
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Summary :
relatively to the two links of the post #12 I do not seem to have made mistakes.
In any event enclose the project complete test that only works if you use a simple php page. In all other cases fails although I followed a logical path, I think.
I remember that my goal is to run python script *.py is that the bash shell *.sh on a web server.
Thank @lemonisdead if you would continue to follow.
B4X:
Sub Globals
     Dim                SiteNoIp As String
     Dim job2             As HttpJob
     Dim lb               As Label
     Dim ButtonTest        As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '========================================
    SiteNoIp = "http://nomesito.no-ip.com/"
    '========================================
    Activity.Title = "How to run scripts .py and .sh"
    Activity.Color = Colors.White
    ButtonTest.Initialize ("ButtonTest")
    Activity.AddView (ButtonTest,5dip,20dip,140dip, 60dip)
    ButtonTest.text = "TEST"
    lb.Initialize ("Lb")
    Activity.AddView (lb,5dip,120dip,200dip,100dip)
    lb.Text = "......."
    lb.TextColor = Colors.Black : lb.color = Colors.white
   
End Sub

Sub ButtonTest_click
    job2.Initialize("Job2", Me)
    '   
    '--- test 1 : OK
    'job2.PostString(SiteNoIp & "hello.php", "a")
    ' hello.php
    '<?php
    ' Print "Hello, World!\n in /var/www/html";
    '?>
   
    '--- test 2 : almost OK 
    'job2.PostString(SiteNoIp & "hello.sh", "a")
    ' hello.sh
    '    #!/bin/bash         
    '    echo Hello World
   
    '--- test 3 :  NO
    job2.PostString(SiteNoIp & "print2.php", "p1=first value&p2=value2")
    ' print2.php
    '    <?php
    '$p1 = $_GET["p1"];
    '$p2 = $_GET["p2"];
    'print ("I've received $p1 and $p2");
    '?>
   
    '--- test 4 : NO
    job2.PostString(SiteNoIp & "testwrite.php", "")
    ' testwrite.php
    '<?php
    'echo shell_exec('testwrite.sh');
    '?>
    '
    ' testwrite.sh
    '#!/bin/bash
    'echo OK > testwrite.txt
    '
    ' testwrite.txt
    ' Lorem ipsum dolor sit amet,
   
   
End Sub

Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  If Job.Success = True Then
      Select Job.JobName
        Case "Job2"
            Log(Job.GetString)
            lb.Text = Job.GetString
      End Select
  Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release
End Sub
 

Attachments

  • RunScript.zip
    7.2 KB · Views: 247
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello @AlpVir ,
I am sorry for the delay.

After having studied the code provided at test_3, I see you use a job.PostString which is sending a POST request. But in your PHP code, you are expecting a GET request ($_GET["p1"];). So you should write $p1 = $_POST["p1"]; instead. Or your PHP script won't receive any parameter sent.
I will come back to you for test_4
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
In test_4, after having prototyped it (not the B4A part), the only things I do think about :
- have you checked the permissions in the directory the Apache's user should write in
- the destination file 'testwrite.txt' has no full path and on one of my VPS it was not written into the /var/www/html but into the ./tmp directory

I will have to prototype the B4A code now.
Edit : played with your code and it was working properly. So, you would have to :
- change the PHP code to get the parameters from the POST request
- check the permissions of the folder where the PHP script is writing
- check the permissions of the bash script too
 
Last edited:
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Concerning the Test 3 the result is positive;
1) Job.Success = True
2) Job.GetString returns "Ive receided first value and value2"

Concerning the Test 4 the result is NOT positive. The file testwrite.txt not written.
1) statement sudo /var/www/html/testwrite.sh writes "OK" in testwrite.txt
2) folder /var/www/html has permissions 0777; testwrite.php and testwrite.sh 0755; testwrite.txt 0666. So it all seems OK.
3) writing
B4X:
<? php
echo shell_exec ('sudo /var/www/html/testwrite.sh');
echo test
  ?>

Job.GetString returns "test"
4) writing
B4X:
#! / bin / bash
echo "OK"> /var/www/html/testwrite.txt
does not lead to any result.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Test 5 (the real one) should start gphoto2 and take a picture ("una foto in Italian language")
A)
B4X:
<?php
echo shell_exec('/var/www/html/unafoto.sh');
echo "one photo"
?>
B)
B4X:
#!/bin/bash
sudo gphoto2 --capture-image-and-download --force-overwrite --filename /var/www/html/unafoto.jpg

Absolutely negative result ! :(:(:(
Needless to say /var/www/html/foto.sh creates a picture.
 
Upvote 0
Top