问题
How can I follow a tutorial like this without using the command line?
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/
I need to be able to use websockets but have a shared server and no access to the command line.
I cannot simply run the script from a browser, if the script would stop running when i closed the browser/disconnected from the internet.
回答1:
Assuming your shared hosting provider supports crontab and cron jobs (most of them do), add the following job to the crontab:
@reboot nohup php /path/to/server.php 2>&1 >/dev/null &
Additionally, you need to start it now, so simply create the following PHP file and access it once, in your browser:
<?php shell_exec('nohup php /path/to/server.php 2>&1 > /dev/null &');?>
That should do the trick. Hopefully your shared hosting provider allows execve() calls to be made. Good luck!
回答2:
Well try run Server.php on browser. Most of the time a php-cli-script also works fine on browser (Until using some command-line only functionality like argv/argc etc. ). On other tab of browser on in another browser you can run usual url like screenshot in given tutorial.
One important thing, Please check socket and other required extensions is enabled or not on your shared server.
回答3:
I didn't know about CRON's @reboot
directive until just now, so @Octav O solution is probably much better. But while testing, I found the following code (found on Saran's websocket tutorial) quite useful.
Save this as websocket_launch.sh
in the same dir as server.php
...
PID=`ps -aef | grep "server.php" | grep -v grep | awk '{print $2}'`
if [ -z $PID ]
then
#echo "Launching now"
nohup php server.php > error_log &
else
#echo "Running as PID $PID"
fi
... then run the shell script with CRON every minute. The script checks if server.php is running, and if not, launches it.
来源:https://stackoverflow.com/questions/14888990/setting-up-websockets-without-command-line