Skip to content
Advertisement

How to run a shell script from a html page on raspberry Pi

I’m trying to have a button on a webpage, hosted on my raspberry Pi, shutdown a QNAP via a script.

The script works when I run it from the pi command line. I’ve tested it. I can also see the html page is working, to a point where the key is added to the url when I click the button.

The script does not run when I press the button on the webpage.

The webpage is hosted using NGINX. This is my NGINX.config

http {
    server {
        listen <myip>:80;
        root /var/www/control;
        index index.html;

        server_name <myWebAddress> www.<myWebAddress>;

        location / {
            try_files $uri $uri/ =404;
        }
    }
}

This is my index.html page

<!DOCTYPE html>
<html style="font-size: 16px;">
  <head>
    <title>Home</title>
   </head>
  <body>
  <header>
  
 <?php
if ($_GET['poweroffqnap']) {
  exec("/etc/control/scripts/poweroffqnap.sh");
}
?>
  <h1>control</h1>
    </header>
    <section>
    <div>
        <a href="?poweroffqnap=true">shutdown QNAP</a>
      </div>
    </section>
  </body>
</html>

all folders and scripts are 755 and owned by root

I run ‘ps aux’ and see the following:

root     28221  0.0  0.1  49472  1172 ?        Ss   13:46   0:00 nginx: master process /usr/sbin/nginx -g daemon on; mas
www-data 28222  0.0  0.3  49628  3372 ?        S    13:46   0:00 nginx: worker process
www-data 28223  0.0  0.2  49628  2616 ?        S    13:46   0:00 nginx: worker process
www-data 28224  0.0  0.2  49628  2616 ?        S    13:46   0:00 nginx: worker process
www-data 28225  0.0  0.2  49628  2616 ?        S    13:46   0:00 nginx: worker process

I think this might be related to user permissions for www-data. I’m not sure how to do any further troubleshooting. I know how to add permissions for a user.

Is my code correct and how can I check what user the web-page us running with, to ascertain if the user can’t execute the script?

Advertisement

Answer

This is my index.html page

Should it not be index.php

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement