简体   繁体   中英

PHP Bash Script calling another Bash script

I have a bash script that takes a parameter is called in PHP by shell_exec(script.sh parameter). Basically, my goal is to call a script that is owned by another user that is not apache.

The script.sh script is a file that contains the following (right now there are some error handling commands):

#/bin/bash
whoami>>whoami
echo $1 >> parameter
while read f; do
env>>envoutput
sudo -i -u archivescriptowner /path/to/archivescript.sh -command archive >> output
done < $1

In my /etc/sudoers file , I have the following:

apache ALL=(archivescriptowner) NOPASSWD: /bin/bash -c /path/to/archivescript.sh  * 

When I run this script as by running su -s /bin/bash apache and pass a parameter, it works.

When I run it via my button in php, archivescript.sh does not execute

The whoami file has apache written to it

The parameter file has the right file written to it

env shows the following

Term=xterm
LD_LIBRARY_PATH=/path/to/library
PATH=/sbin/:usr/sbin:/bin:/usr/bin
PWD=/var/www/html
LANG=C
SHLVL=4
=/bin/env

PWD is outputting right, that is where my script is right now, it will be moved in the future.

The output file when it is ran by the button click is blank.

I am at a loss as to why this is not working. Any insight would be helpful. Please let me know if I need to give any additional information.

I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);

$return1  = $shell->exeCmd('/path/to/archivescript.sh');

echo $return1; //return from your script

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