kyo*_*kyo 7 unix apache bash shell nginx
我在 Ubuntu VM 上部署了一个 Laravel 项目。我有一个我现在正在处理的脚本,用于了解当前的 VM 是使用 nginx 还是 Apache 以编程方式部署的。
我知道我可以检查使用这些ps
和grep
命令我会发现
root@theawsbiz:~# ps -aux | grep apache
root 3446 0.0 1.8 544540 37144 ? Ss 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3449 0.1 1.9 550388 39796 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3454 0.0 1.0 547336 21532 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3457 0.0 1.8 548196 37864 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3458 0.0 1.0 547320 21428 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3462 0.0 1.7 550008 36264 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3465 0.0 1.8 550408 38160 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3466 0.0 1.9 550400 40512 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3467 0.0 1.0 547320 21416 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3468 0.0 1.7 548228 36236 ? S 17:02 0:00 /usr/sbin/apache2 -k start
www-data 3520 0.0 0.9 546872 19964 ? S 17:06 0:00 /usr/sbin/apache2 -k start
root 3526 0.0 0.0 14856 1036 pts/1 S+ 17:06 0:00 grep --color=auto apache
root@theawsbiz:~# ps -aux | grep nginx
root 3529 0.0 0.0 14856 1092 pts/1 S+ 17:06 0:00 grep --color=auto nginx
root@theawsbiz:~#
Run Code Online (Sandbox Code Playgroud)
有了这些结果,我知道这个 VM 正在使用 Apache。
但是,我不知道如何通过 Bash 脚本检查它。一个人会怎么做呢?我现在愿意接受任何建议。
Son*_*nny 13
由于您正在尝试使用grep
and来实现这一点ps
,因此您可以执行以下操作:
if [[ `ps -acx|grep apache|wc -l` > 0 ]]; then
echo "VM Configured with Apache"
fi
if [[ `ps -acx|grep nginx|wc -l` > 0 ]]; then
echo "VM Configured with Nginx"
fi
Run Code Online (Sandbox Code Playgroud)
$ curl -v api.company.co.ke 2>&1 |grep -i server | awk -F: '{print $2}'
nginx/1.10.3
You can run the command in a subshell and get the output
? get_server_version=$(curl -v api.company.co.ke 2>&1 |grep -i server | awk -F: '{print $2}')
? echo $get_server_version
nginx/1.10.3
Run Code Online (Sandbox Code Playgroud)
或者直接运行 pgrep
? { pgrep nginx && server_version="nginx"; } || { pgrep apache && server_version="apache"; } || server_version="unknown"
# On server running nginx
? echo $server_version
nginx
# On server with neither nginx nor apache
? echo $server_version
unknown
Run Code Online (Sandbox Code Playgroud)