简体   繁体   中英

Running shell script from php and then php script from shell, can't get it to work

I'm running an .sh script from php with exec (tried shell_exec and passthru too) and appending the command with > /dev/null 2>&1 & so the php script frees up the browser. Then in the shell script I'm running another php file and capturing the response of that php file into a variable. The shell script ends up calling index.php on the server instead of the supplied php file.

Here's my php command to run the shell script:

exec(BASE.'/../shellfile.sh '.$arg1.' '.$arg2.' '.$arg3.' > /dev/null 2>&1 &');

And here's my command to run a php file from the shell script:

VAR1=$(php -q $BASE/processingfile.php $FOLDER/temp.txt)

Side notes:

BASE is defined in PHP script and $arg1 , $arg2 and $arg3 are defined variables.

VAR1 , BASE and FOLDER are defined in the shell script.

I tried running BASE.'/../shellfile.sh '.$arg1.' '.$arg2.' '.$arg3.' > /dev/null 2>&1 &' BASE.'/../shellfile.sh '.$arg1.' '.$arg2.' '.$arg3.' > /dev/null 2>&1 &' BASE.'/../shellfile.sh '.$arg1.' '.$arg2.' '.$arg3.' > /dev/null 2>&1 &' from Putty with SSH and it works well. It includes correct file instead of index.php if ran directly from SSH.

So this is the logic: PHP -> Run shell file and free-up browser, Shell -> Run PHP file and capture response.

Shell script ends up calling index.php on the server instead of processingfile.php . Besides, PHP headers like X-Powered-By: PHP/5.X.XX are returned to shell script from index.php and saved into the VAR1 shell variable.

Any suggestions on what I'm doing wrong?

Additional notes:

  • First PHP file is located below public folder, whereas the shell script and processingfile.php are located above the public folder

  • I have an .htaccess file in public folder, but not above public

  • shellfile.sh and processingfile.php are chmoded 777.

You may need to change from the default directory (eg the directory where index.php is) to the directory to where you want to script to run. For example, use a command similar to this to tell php where to look for the script.

exec("cd path/to/file && sh ./yourScript.sh);

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