如何在Redhat下将Shell脚本作为守护程序运行?


12

我有一个shell脚本,它实际上是一个带有一些日志记录的线性脚本,我正在尝试从init脚本中运行它。我正在使用其中的daemon函数/etc/init.d/functions来运行它,因为Redhat似乎不可start-stop-daemon用。当我调用初始化脚本(/etc/init.d/script start)时,它停留在前台,而不是完成并使进程运行。我将该守护程序守护起来的正确方法是什么?

要运行的脚本:

# conf file where variables are defined
. /etc/script.conf

echo "Starting..." | logger -i
echo "Monitoring $LOG_LOCATION." | logger -i
echo "Sending to $MONITOR_HOST:$MONITOR_PORT." | logger -i

tail -n 1 -F $LOG_LOCATION |
grep WARN --line-buffered  |
/usr/bin/nc -vv $MONITOR_HOST $MONITOR_PORT 2>&1 |
logger -i

初始化脚本:

#!/bin/bash


# Source Defaults
. /etc/default/script

# Source init functions
. /etc/init.d/functions

prog=/usr/local/bin/script.sh

[ -f /etc/script.conf ] || exit 1

RETVAL=0

start()
{
    # Quit if disabled
    if ! $ENABLED; then
            echo "Service Disabled in /etc/default/script"
            exit 1
    fi

    echo "Starting $prog"

    daemon $prog

    RETVAL=$?

    return $RETVAL
}

stop ()
{
    echo -n $"Stopping $prog: "
    killproc $prog

    RETVAL=$?

    return $RETVAL
}

reload()
{
    echo "Reload command is not implemented for this service."
    return $RETVAL
}

restart()
{
    stop
    start
}

condrestart()
{
    echo "Not Implemented."
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status $prog
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    condrestart)
        condrestart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        RETVAL=1
esac

使用bash -vx的最后20行执行:

+ case "$1" in
+ start
+ true
+ echo 'Starting /usr/local/bin/script.sh'
Starting /usr/local/bin/script.sh
+ daemon /usr/local/bin/script.sh
+ local gotbase= force=
+ local base= user= nice= bg= pid=
+ nicelevel=0
+ '[' /usr/local/bin/script.sh '!=' /usr/local/bin/script.sh ']'
+ '[' -z '' ']'
+ base=script.sh
+ '[' -f /var/run/script.sh.pid ']'
+ '[' -n '' -a -z '' ']'
+ ulimit -S -c 0
+ '[' -n '' ']'
+ '[' color = verbose -a -z '' ']'
+ '[' -z '' ']'
+ initlog -q -c /usr/local/bin/script.sh

对我来说,运行该脚本bash -vx ...并张贴最后几行似乎很有用,这样我们就可以看到保留在前台的内容。
Hauke Laging

1
不要理会这个用法的正确性,并且daemon也有一个RPM软件包。顺便说一句,那里有许多日志监视工具(从此处开始)。
sr_

Hauke,您是说使用第一行#!/bin/bash -vx吗?我尝试这样做,但是与直接运行shell脚本时相比,它不会从init脚本中产生相同的输出。
bshacklett

@bshacklett您可以通过使用显式运行任何初始化脚本(实际上是任何外壳脚本)的功能来检查它bash -vxbash -vx /etc/init.d/script start
sr_

1
@bshacklett Wrt日志,我会仔细看看logstash。可以直接从Log4j向存储提供日志,但是logstash代理还可以监视日志文件
sr_

Answers:


2

我发现在脚本http://www.linuxforums.org/forum/programming-scripting/190279-daemon-etc-init-d-functions-does-not-return-launching-process.html#post897522我能修改以满足我的需求。它手动跟踪PID并使用来创建PID文件pidof。我最终不得不修改它以使用pgreppidof因为无法看到我脚本的PID。修改之后,它运行良好。*注意,pgrep似乎仅在完整脚本名称少于15个字符时才起作用

我最终得到的是:

#!/bin/bash
#
# 
#
# Start on runlevels 3, 4 and 5. Start late, kill early.
# chkconfig: 345 95 05
#
#
#!/bin/bash

# absolute path to executable binary
progpath='/usr/local/bin/script.sh'

# arguments to script
opts=''

# binary program name
prog=$(basename $progpath)

# pid file
pidfile="/var/run/${prog}.pid"

# make sure full path to executable binary is found
! [ -x $progpath ] && echo "$progpath: executable not found" && exit 1

eval_cmd() {
  local rc=$1
  if [ $rc -eq 0 ]; then
    echo '[  OK  ]'
  else
    echo '[FAILED]'
  fi
  return $rc
}

start() {
  # see if running
  local pids=$(pgrep $prog)

  if [ -n "$pids" ]; then
    echo "$prog (pid $pids) is already running"
    return 0
  fi
  printf "%-50s%s" "Starting $prog: " ''
  $progpath $opts &

  # save pid to file if you want
  echo $! > $pidfile

  # check again if running
  pgrep $prog >/dev/null 2>&1
  eval_cmd $?
}

stop() {
  # see if running
  local pids=$(pgrep $prog)

  if [ -z "$pids" ]; then
    echo "$prog not running"
    return 0
  fi
  printf "%-50s%s" "Stopping $prog: " ''
  rm -f $pidfile
  kill -9 $pids
  eval_cmd $?
}

status() {
  # see if running
  local pids=$(pgrep $prog)

  if [ -n "$pids" ]; then
    echo "$prog (pid $pids) is running"
  else
    echo "$prog is stopped"
  fi
}

case $1 in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart)
    stop
    sleep 1
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit $?

0

我不认识Redhat,但daemon $prog &对我来说很奇怪。如果已经有一个守护程序,为什么必须将这个函数本身放在后台(并且很有用)?因此,尝试不使用&


4
没错 /etc/init.d/functions定义了一个daemon函数,该函数期望其参数来守护自己,它仅处理诸如更改用户,设置ulimits,检查(不创建!)pidfile之类的事情……此daemon函数的最佳用法是将其替换为libslack的daemon ;)
sr_

抱歉,&排查故障时,&当时在那儿。我并不是要在这篇文章中包括它。
bshacklett
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.