如何在Redhat下运行shell脚本作为守护进程?

Modified on: Sat, 30 Jun 2018 05:28:30 +0800

我有一个shell脚本,它本质上是一个带有一些日志记录的单行程序,我试图从init脚本运行它。我使用daemon函数的/etc/init.d/functions的内部运行它,因为红帽不会出现有启动 - 停止守护可用。当我调用init脚本(start-stop-daemon)时,它会保留在前台,而不是完成并让流程继续运行。让这个脚本守护的正确方法是什么?

要运行的脚本:

# 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

init脚本:

# 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

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

#!/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
作者:,bshacklett

最佳答案

我在http://www.linuxforums.org/forum/programming-scripting/190279-daemon-etc-init-d-functions-does-not-return-launching-process.html #post897522我能够根据自己的需要进行修改。它手动跟踪PID并使用pidof创建PID文件。我最终不得不修改它以使用pgrep,因为pidof无法看到我的脚本的PID。经过修改后,它工作正常。 *注意,如果完整的脚本名称长度不超过15个字符,pgrep似乎只能工作

以下是我最终的结果:

#!/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 $?

作者:bshacklett

相关问答

添加新评论