simonuzzo
Posts: 2
Joined: Sat May 04, 2019 5:51 pm

Launch sh script from php page

Sat May 04, 2019 5:58 pm

Hello,
I have a problem. I should run a sh script that I created, in turn this script starts an application with its own webserver.
Since I have more webservers I would like to manage from php, the sh script related to the webserver I need.
The scripts work normally from the terminal, but from php they do not start anything.
How can I do?

Code: Select all

sh script
sleep 0.5
cd /home/pi/gphoto2-bottle
sleep 0.2
nohup sudo ./gphoto2-bottle.py &

Code: Select all

php code
<?php
$Test = shell_exec('/home/pi/myfile.sh');
echo $Test;
?><br><br>

User avatar
DaveyDave1999
Posts: 28
Joined: Tue Apr 16, 2019 9:16 pm
Location: United States, Hawaii

Re: Launch sh script from php page

Sat May 04, 2019 10:42 pm

Your shell_exec function should look like this:

Code: Select all

$output = shell_exec("sh your-script.sh");
Keep in mind that this is a HUGE security breach! Dont use it unless you are making a private server!
Dont forget to make the script executable.
Good luck!
Blog about Raspberries coming soon...

simonuzzo
Posts: 2
Joined: Sat May 04, 2019 5:51 pm

Re: Launch sh script from php page

Sun May 05, 2019 1:15 am

Thanks for the reply,
I have not tried with your code, but out of curiosity, as soon as I have a second I try it.

currently I have solved "simply" by giving sudo to the command.
and autorize the script to the sudoers file for apache user.

as the intended use is private and offline.

but I have to try your method, which looks like it doesn't need sudo autorizzation.

Return to “Beginners”