简体   繁体   中英

Issues With PHP exec & shell_exec - Shell Script Execution

I've been unable to run php scripts that I need to use to start and stop webcam services that run on the local machine with the scripts. I can find nothing in the logs to indicate why the script doesn't' work.

I confess to being severely handicapped regarding PHP, especially server-side scripting.

The environment is Debian Jesse running Nginx with all required SSH and PHP modules installed

I have added www-data to the sudoers file with:

    www-data ALL=(ALL) NOPASSWD: /var/www/html/start_webcam.sh

Enabled the $PATH environment for www-data at:

    /etc/php5/fpm/pool.d/www.conf

The shell script resides in the .../html directory and runs from the terminal with no issues.

This is the code for both the php and shell scripts:

start_webcam.php:

    <?php
    echo exec('sudo bash /var/www/html/aspirebox/start_webcam.sh 2>&1, $output');
    print_r($output);
    ?>

The $output and print_r stuff is there because it was the last thing I tried based on a post I found out here somewhere.

start_webcam.sh

    #!/bin/bash
    service motion start

Thanks in advance to anyone out here that has a clue. After 2 days of wrestling with this, I am sure that I do not.

according to Passing Variables to shell_exec()? you should change your code like this:

<?php
$output = exec('/var/www/html/aspirebox/start_webcam.sh 2>&1 ');
print_r($output);
?>

and let your bash script execute as all (no need to sudo bash):

chmod a+x /var/www/html/aspirebox/start_webcam.sh

Thank you very much - that worked.

I worked through getting the path straight for the directory the shell script runs in, and the correct path to run "service".

All I have now is to figure out why I'm getting "Failed to start motion.service: Access denied"

I've given www-data permission to run the script without a password on sudoers, have to keep digging.

Thanks again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM