Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute shell command with php

Tags:

bash

shell

php

im trying to execute a bash script with a php/html button to wake my nas.

<form action="" method="POST">
    <input type="submit" value="Wake NAS" name="zero" />
</form>
<?php
  if (isset($_POST["zero"])){
    #echo "Hello World!";
    shell_exec("/var/www/html/wakenas.sh &");
  }?>

"Hello World" is printed when button is pressed. but code won't be executed. the wakenas.sh looks like this and works if i execute it over shell

#!/bin/bash
etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1

wakenas.sh has all rights

Maybe you guys know why it wont be executed.

thanks in advance

like image 542
Gutz-Pilz Avatar asked Jul 12 '26 19:07

Gutz-Pilz


1 Answers

The easy and secure way of executing your script is to put in sudoers. Assuming your Linux distribution is Debian base and user of who run the web server is www-data, then you can create a file e.g /etc/sudoers.d/wakeup_ether

Cmnd_Alias WAKE_UP_CMD = /var/www/html/wakenas.sh
www-data ALL=NOPASSWD: WAKE_UP_CMD

Modify your script to prefix the command with sudo.

shell_exec("sudo /var/www/html/wakenas.sh &");

Reference: https://help.ubuntu.com/community/RootSudo

like image 177
rioastamal Avatar answered Jul 15 '26 15:07

rioastamal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!