我有一个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
Hauke,您是说使用第一行
—
bshacklett
#!/bin/bash -vx
吗?我尝试这样做,但是与直接运行shell脚本时相比,它不会从init脚本中产生相同的输出。
@bshacklett您可以通过使用显式运行任何初始化脚本(实际上是任何外壳脚本)的功能来检查它
—
sr_
bash -vx
。bash -vx /etc/init.d/script start
。
bash -vx ...
并张贴最后几行似乎很有用,这样我们就可以看到保留在前台的内容。