Linux

systemd 無法辨識 pid 文件

  • February 22, 2018

所以我試圖在啟用 systemd 的系統上啟動服務。服務名稱是ossec-hids-authdossec(入侵檢測軟體)中的身份驗證引擎(代理)。當我開始啟動 init 腳本時,systemctl 超時並在獲取狀態時看到此錯誤。

/etc/init.d/ossec-hids-authd status
● ossec-hids-authd.service - LSB: Authentication Daemon for OSSEC-HIDS.
  Loaded: loaded (/etc/rc.d/init.d/ossec-hids-authd; bad; vendor preset: disabled)
  Active: failed (Result: timeout) since Thu 2018-02-22 07:34:28 UTC; 11min ago
    Docs: man:systemd-sysv-generator(8)

Feb 22 07:24:11 ip-10-0-197-117.ec2.internal systemd[1]: Starting LSB: Authentication Daemon for OSSEC-HIDS....
Feb 22 07:24:11 ip-10-0-197-117.ec2.internal ossec-hids-authd[21142]: [39B blob data]
Feb 22 07:24:11 ip-10-0-197-117.ec2.internal systemd[1]: PID file /var/run/ossec-authd.pid not readable (yet?) after start.
Feb 22 07:24:11 ip-10-0-197-117.ec2.internal ossec-hids-authd[21142]: 2018/02/22 07:24:11 ossec-authd: INFO: Started (pid: 21148).
Feb 22 07:34:28 ip-10-0-197-117.ec2.internal systemd[1]: ossec-hids-authd.service start operation timed out. Terminating.
Feb 22 07:34:28 ip-10-0-197-117.ec2.internal systemd[1]: Failed to start LSB: Authentication Daemon for OSSEC-HIDS..
Feb 22 07:34:28 ip-10-0-197-117.ec2.internal systemd[1]: Unit ossec-hids-authd.service entered failed state.
Feb 22 07:34:28 ip-10-0-197-117.ec2.internal systemd[1]: ossec-hids-authd.service failed.
Feb 22 07:40:20 ip-10-0-197-117.ec2.internal ossec-hids-authd[21142]: 2018/02/22 07:40:20 ossec-authd(1225): INFO: SIGNAL [(15)-(Terminated)] Received. Exit Cleaning...

現在在初始化腳本中,這個過程實際上是在製作 pid 文件,/var/ossec/var/run而不是/var/run我檢查了 pid 文件實際上是在那裡創建的。但不知何故 systemctl 無法辨識它。

systemd 是否有可能無法辨識在外部創建的 pid 文件/var/run,如果是這種情況怎麼辦?

下面是初始化腳本

#!/bin/sh
#
# ossec-authd  Start the OSSEC-HIDS Authentication Daemon
#
# chkconfig: 2345 99 01
# description: Provides key signing for OSSEC Clients
# processname: ossec-authd
# config: /var/ossec/etc/ossec.conf
# pidfile: /var/run/ossec-authd.pid
### BEGIN INIT INFO
# Provides:          ossec-authd
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Authentication Daemon for OSSEC-HIDS.
# Description:       Provides key signing for OSSEC Clients
### END INIT INFO

# Author: Brad Lhotsky <brad.lhotsky@gmail.com>
NAME=ossec-authd
DAEMON=/var/ossec/bin/ossec-authd
DAEMON_ARGS="-p 1515 2>&1 >> /var/ossec/logs/ossec-authd.log &"
PIDDIR=/var/ossec/var/run
SCRIPTNAME=/etc/init.d/ossec-authd

. /etc/rc.d/init.d/functions

getpid() {
   for filename in $PIDDIR/${NAME}*.pid; do
       pidfile=$(basename $filename)
       pid=$(echo $pidfile |cut -d\- -f 3 |cut -d\. -f 1)
       kill -0 $pid &> /dev/null
       RETVAL=$?
       if [ $RETVAL -eq 0 ]; then
           PIDFILE=$filename
           PID=$pid
       else
           rm -f $filename
       fi;
   done;
}

start() {
 echo -n $"Starting $NAME: "
 daemon $DAEMON $DAEMON_ARGS
 retval=$?
 if [ $retval -eq 0 ]; then
   echo_success
   echo
 else
   echo_failure
   echo
 fi
 return $retval
}

stop() {
 echo -n $"Stopping $NAME: "
 getpid
 killproc -p $PIDFILE $NAME
 retval=$?
 echo
 return $retval
}

restart() {
 stop
 start
}

case "$1" in
 start)
   start
   ;;
 stop)
   stop
   ;;
 status)
   getpid
   if [ -z $PIDFILE ]; then
       status $NAME
   else
       status -p $PIDFILE $NAME
   fi;
   ;;
 restart)
   restart
   ;;
 *)
   echo "Usage: $0 {start|stop|status}"
   exit 2
   ;;
esac

exit $?

systemd 解析初始化腳本的註釋以在啟動時或daemon-reload命令時生成臨時 .service 文件。換行

# pidfile: /var/run/ossec-authd.pid

# pidfile: /var/ossec/var/run/ossec-authd.pid

並執行systemctl daemon-reload

UPD:現在我看到 pid 文件名是由 authd 在執行時生成的,並且 init 腳本必須搜尋 $ PIDDIR/ $ {NAME}*.pid。

Systemd 無法搜尋 pidfile,但沒有它也可以工作。所以你可以嘗試# pidfile:完全刪除行,或者編寫你自己的 .service 文件

引用自:https://unix.stackexchange.com/questions/425834