我在 CentOS 6.5 中有一个 TCL 脚本,我以 root 身份从 shell 运行,没有任何问题。但如果我将它作为 init.d 的服务运行,它就会失败。这是 init.d 脚本:
#!/bin/bash
#
# camelot Camelot 11.5.0
#
# Source function library.
. /etc/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
CAMELOT_LOGS=/var/camelot/logs
CAMELOT_LIB=/usr/local/camelot/lib
RETVAL=0
prog="camelot"
LOCKFILE=/var/lock/subsys/$prog
# Declare variables for service
start() {
echo -n "Starting $prog: "
/opt/camelot/register-phones.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCKFILE
echo
return $RETVAL
}
stop() {
echo -n "Shutting down $prog: "
killall screen
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
echo
return $RETVAL
}
status() {
echo -n "Checking $prog status: "
echo -n "Sorry, not implemented yet. run 'screen -r' to check on the process."
RETVAL=$?
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
;;
*)
echo "Usage: $prog {start|stop|status|restart}"
esac
exit $RETVAL
这是我收到的错误消息:
Starting camelot: camelot server at localhost:6060 is inaccessible
while executing
"error "camelot server at $server:$port is inaccessible""
(procedure "dorpc" line 122)
invoked from within
"dorpc $server $port $outmsg"
(procedure "createendpoint" line 7)
invoked from within
"createendpoint $server $port 0 "$type $args@""
(procedure "camelot::newendpoint" line 10)
invoked from within
"camelot::newendpoint $CamelotServerIp $CamelotServerPort sipx SEP$ep2MAC"
(procedure "registerPhone" line 22)
invoked from within
"registerPhone $data"
("while" body line 3)
invoked from within
"while {$data != ""} {
# puts $data
puts [registerPhone $data]
gets $fp data
}"
(file "/opt/camelot/register-phones.sh" line 7)
看起来 TCL 无法运行 dorpc,但这个端口肯定是打开的 - 我可以在此处失败后立即从 shell 运行该脚本,并且运行得很好。我相信我已经设置了从 printenv 作为 root 看到的所有相关环境变量(无论是否在 init.d 脚本中设置环境变量,它都会出现相同的错误)。
关于 init.d 和 TCL 有什么我遗漏的吗?
答案1
我的朋友帮我解决了这个问题 - 我们使用 bash -l -c 来获得一个完整的登录 shell 来运行 TCL 脚本,它显然更喜欢这样:
start() {
echo -n "Starting $prog: "
/bin/bash -l -c '/opt/camelot/register-phones.sh'
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCKFILE
echo
return $RETVAL
}