我们之前学过的进程查看命令有ps、top,但因为top是动态进程查看命令,所以选择ps吧。
比如我们要查看nginx进程是否存在:
[root@server1 shells]# ps -aux |grep nginx
root 21535 0.0 0.0 112704 1024 pts/0 S+ 20:08 0:00 grep --color=auto nginx
很明显,进程中没有nginx的进程,唯一一个是因为我们打开了grep --color=auto nginx
命令才出现的进程。如果根据进程状态返回值,能判断进程中没有嘛?
[root@server1 shells]# echo $?
0 //返回值为0,说明这个进程返回值正常。但进程中没有nginx服务啊。。。。
这个时候我们引入pgrep命令:
直接上命令:
#!/bin/bash
pgrep nginx
if [ "$?" -eq 0 ]
then
echo "Be in"
else
echo "Not in"
fi
看看运行结果:
[root@server1 shells]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
[root@server1 shells]# sh jj httpd
Not in
打开服务以后:
[root@server1 shells]# systemctl start httpd
[root@server1 shells]# sh jj httpd
Be in