问题原文
原帖链接 · jgivoni · CC BY-SA 3.0
Recent versions of PHP has a built-in webserver (for non-production environments) that can be started with php -S [...].
How can I check if it's already running on the server before attempting to start it?
I realise that the hacky way would be to try to start it and look out for the error message Failed to listen on [...], but I thought there might be a cleaner way :-)
EDIT: I forgot to mention that ultimately I'd like to be able to check this in a script and only start the server if it's not already started. I also forgot to mention that I'm looking for a solution for Linux, but Windows solutions are of course welcome as well, in case someone else will be looking for that one day :-)
已采纳解答
原帖链接 · jgivoni · CC BY-SA 3.0
Since my edit to Paolo's answer was rejected, here's a more complete answer:
ps aux | grep php
This command will output something like the following if php is currently running:
pi 3245 0.0 0.5 21400 2416 pts/0 S Jan29 0:02 php -S localhost:8000 -t /path/to/php/docroot
pi 3518 0.0 0.3 5220 1712 pts/0 S+ 00:42 0:00 grep --color=auto php
The second line can be ignored as it's just self-referencing the search command. This script will check if the server is running and start it if it's not:
if ! ps aux | grep -v grep | grep -qs 'php -S localhost:8000';
then
php -S localhost:8000 -t /path/to/php/docroot &>/dev/null &
fi
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
