用于验证进程是否正在运行的 Bash 脚本不起作用

用于验证进程是否正在运行的 Bash 脚本不起作用

我有一个简单的脚本,如下所示,用于检查失败2ban服务是否在 Ubuntu 18.04 上运行:

#!/bin/bash

# Script to check if fail2ban service is running

if pgrep -x "fail2ban" > /dev/null
then
   echo "Fail2ban is running"
else
   echo "Fail2ban is not running"
fi

我已经在测试虚拟机中安装了 fail2ban 并且正在该虚拟机上运行。以下是屏幕截图systemctl status命令。

但是,当运行上述脚本时,我得到的结果是“Fail2ban 未运行”。我不确定脚本是否是。我ps aux也尝试使用命令代替pgrep。但是,我仍然得到相同的结果

答案1

您要求pgrep精确地(-x)搜索一个称为的进程fail2ban ,但是输出却systemctl status显示它被调用 /usr/bin/python3

要检查某个systemd单元是否正在运行,请使用

systemctl is-active --quiet fail2ban

那是:

if systemctl is-active --quiet fail2ban; then
    echo "running"
else
    echo "not running"
fi

答案2

以下 shellscriptrunning结合了

  • systemctl is-active
  • ps -ef | ... | grep

以检测某个程序(或包含搜索字符串的程序名称)是否正在运行。

#!/bin/bash

if [ $# -ne 1 ]
then
 echo "Usage:    $0 <program-name>
          $0 <part of program name>
Examples: $0 firefox
          $0 term
          $0 dbus
          $0 'dbus-daemon --session'"
 exit
fi

inversvid="\0033[7m"
resetvid="\0033[0m"
redback="\0033[1;37;41m"
greenback="\0033[1;37;42m"
blueback="\0033[1;37;44m"

runn=false
tmpfil=$(mktemp)

# check by systemctl

systemctl is-active --quiet "$1"
if [ $? -eq 0 ]
then
 echo "systemctl is-active:"
 runn=true
fi

# check by ps

ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8-9 | grep "$1" | grep -vE -e "$0 $1" -e "grep $1" | sort -u > "$tmpfil"

tmpstr=$(head -n1 $tmpfil)
#echo "$tmpstr"
if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ]
then
 echo "ps -ef: active:"
 runn=true
elif test -s "$tmpfil"
then
 if $runn
 then
  echo "----- consider also ---------------------------------------------"
  cat "$tmpfil"
  echo "-----------------------------------------------------------------"
 else
  echo "----- try with: -------------------------------------------------"
  cat "$tmpfil"
  echo "-----------------------------------------------------------------"
 fi
fi

if $runn
then
 echo -e "$greenback $1 is running $resetvid"
else
 inpath=$(which "$1")
 if [ "$inpath" == "" ]
 then
  echo -e "$redback no path found to $1 $resetvid"
 else
  echo -e "$blueback $1 is not running $resetvid"
 fi
fi

如果需要,请将其设置为可执行文件,并将其放入 PATH 中的目录中。我将其放入我的bin目录中,无需任何路径即可使用它。

用法:

$ running 
Usage:    /home/sudodus/bin/running <program-name>
          /home/sudodus/bin/running <part of program name>
Examples: /home/sudodus/bin/running firefox
          /home/sudodus/bin/running term
          /home/sudodus/bin/running dbus
          /home/sudodus/bin/running 'dbus-daemon --session'

例子:

$ running firefox
ps -ef: active:
 firefox is running   # green background - running

$ running term
----- try with: -------------------------------------------------
/usr/lib/gnome-terminal/gnome-terminal-server
xterm
x-terminal-emulator
-----------------------------------------------------------------
 no path found to term   # red background - path not found

$ running dbus
systemctl is-active:
----- consider also ---------------------------------------------
/usr/bin/dbus-daemon --session
/usr/bin/dbus-daemon --syslog
/usr/bin/dbus-daemon --system
/usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-Nm2MSvuTZF,guid=25bad8d51276d088045625055c425080
-----------------------------------------------------------------
 dbus is running   # green background

$ running 'dbus-daemon --session'
ps -ef: active:
 dbus-daemon --session is running   # green background

$ running libreoffice
 libreoffice is not running    # blue background - not running

相关内容