sab*_*020 1 shell shell-script
我有一个定制的 linux 版本,内核版本是 4.1.15-klk
我的平台架构是armv7l GNU/Linux
我正在尝试检查我的进程是否正在运行:
我试过这个:
#!/bin/sh
service=myservice
if [ $(ps | grep -v grep | grep $service | wc -l) -gt 0 ]
then
echo "$service is running!!!"
else
echo "$service is not running!!!"
fi
Run Code Online (Sandbox Code Playgroud)
但它给了我“myservice 正在运行!!!” 是否正在运行
我不能使用 pgrep,因为它在我的系统中不可用
有任何想法吗?谢谢!
基于此问题,简短且可编写脚本的方法是:
systemctl is-active --quiet service
Run Code Online (Sandbox Code Playgroud)
如果服务正在运行,它将以代码零退出。
打印 sshd 服务是否正在运行的示例:
systemctl is-active --quiet sshd && echo "sshd is running" || echo "sshd is NOT running"
Run Code Online (Sandbox Code Playgroud)