无法在 FreeBSD 中终止进程

cannot kill process in FreeBSD

我在 FreeBSD 10.1 版本中有一个脚本,它的目的是监视另一个进程并使该进程保持活动状态。 当我试图自杀时,它总是失败。 我试试 killall [name | pid]; pkill -9 [名称];服务 watchtas 停止,其中 none 工作。 下面是我的脚本,请指教解决方法

#!/bin/sh

. /etc/rc.subr
prog="Thin-Agent WatchDog"
TAS_BIN="/etc/supermicro/tas-freebsd.x86_64"
TAS_LOG="/etc/supermicro/tas_system_crush.log"
monitor=1
name="watchtas"
rcvar=${name}_enable
command=/etc/rc.d/{$name}
start_cmd="watchdog"
stop_cmd="stop_watching"
load_rc_config $name


recover_tas() {
  $TAS_BIN -agent start-service
  RETVAl=$?
  return $RETVAL
}

stop_watching() {
  monitor=0
}

watchdog() {
    while [ $monitor == 1 ]
    do
        tas_count=`ps -x | grep tas-freebsd.x86_64 | grep -v grep | wc -l | sed 's/ *//g'`

        if [ $tas_count -eq 0 ]; then
            timestamp=`date`
            echo "[$timestamp]TAS shutdown unexpectedly, restarting TAS now..." >> $TAS_LOG
            echo $?
            recover_tas
        else
            sleep 10
        fi
    done
}

run_rc_command ""

您的启动脚本在几个方面失败了。 service watchtas start 不会 return 到命令行,因为守护进程不会分离。 service watchtas stop 无法按要求工作,因为变量 monitor 是执行脚本的本地变量。

我会把启动脚本和看门狗代码分开放在单独的文件中,然后使用daemon(8)来监控看门狗。

/usr/local/etc/rc.d 启动脚本如下所示:

#!/bin/sh
. /etc/rc.subr
name="watchtas"
rcvar=${name}_enable
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-c -f -P ${pidfile} -r /usr/local/sbin/${name}"
load_rc_config $name
run_rc_command ""

/usr/local/sbin/watchtas 看门狗代码看起来像这样:

#!/bin/sh
TAS_BIN="/etc/supermicro/tas-freebsd.x86_64"
TAS_LOG="/etc/supermicro/tas_system_crush.log"

recover_tas() {
  $TAS_BIN -agent start-service
  RETVAl=$?
  return $RETVAL
}

while true
do
    tas_count=`ps -x | grep tas-freebsd.x86_64 | grep -v grep | wc -l | sed 's/ *//g'`

    if [ $tas_count -eq 0 ]; then
        timestamp=`date`
        echo "[$timestamp]TAS shutdown unexpectedly, restarting TAS now..." >> $TAS_LOG
        echo $?
    recover_tas
    else
        sleep 10
    fi
done

看来你有一个守护进程正在监视一个守护进程正在监视一个守护进程。