running bash scripts in php
I have two computers. On the first computer I have apache running with all my web code. On the second computer I have large amounts of data stored with a retrieval script (the script usually takes hours to run). I am essentially creating a web UI to access this data without any time delay.
so I call:
exec("bash initial.bash");
this is a driver script that is in my Apache folder. It calls the script on the other computer.
calling:
ssh otherMachine temp.bash&
this script invokes the data retrieval script on the second computer.
If I call initial.bash in the terminal, everything works smoothly and successfully, but if I c开发者_高级运维all it in my PHP file, then all my commands in initial.bash run, with the exception of ssh otherMachine temp.bash&
. I put the & at the end of that, so that temp.bash will run in the background, since it does take a few hours to complete.
I am not sure why the nested script is not running when invoked by Apache. Is there a better alternative than using exec or shell_exec to call a script, which ultimately calls another script. The reason I don't call a script on the second machine directly is because of the time it takes the program to run. Shell_exec does not render the php page until the script is complete.
A couple suggestions:
Try putting the full path to ssh (
/usr/bin/ssh
or wherever it's installed instead of justssh
)Make sure the user apache/PHP is running under is set up correctly to ssh to othermachine without asking for a password. Obviously if SSH asks for a password, there is nobody there to type it in, so it will just appear to have done nothing.
Try redirecting outputs as well --
ssh othermachine temp.bash >/dev/null 2>&1 &
Try some different flags to ssh. Like
-n
which is specifically designed for running a command over ssh in the background.
My guess is that SSH doesn't like the user php is running under? You say it works fine from the terminal - are you logged in as the same php user?
You might also find a message queue of some sort a better solution.
You may use cron (scheduled) jobs. Cron job retrieve the data periodically via using your bash script.
Additionally try to exec("/bin/bash /path/to/script.sh"); Maybe php does not find the path of bash. You have to look apache logs for additional info.
精彩评论