bash start-stop-daemon 在命令行中工作,但在 /etc/init.d 脚本中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5231036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
start-stop-daemon works at command line but doesn't work in /etc/init.d script
提问by Max Williams
I'm trying to get a starter script (for a ruby gem called ar_sendmail) working in /etc/init.d/ar_sendmail:
我正在尝试获取在 /etc/init.d/ar_sendmail 中工作的启动脚本(用于名为 ar_sendmail 的 ruby gem):
#! /bin/sh
echo "in /etc/init.d/ar_sendmail"
DIR=/home/max/work/e_learning_resource/trunk
PATH=/var/lib/gems/1.8/bin
DAEMON=/var/lib/gems/1.8/bin/ar_sendmail
DAEMON_OPTS="-e production -d --batch-size 100 --delay 150"
NAME=ar_sendmail
DESC=ar_sendmail
PID_FILE=/home/max/work/e_learning_resource/trunk/shared/log/ar_sendmail.pid
test -x $DAEMON || exit 0
set -e
case "" in
start)
echo -n "Starting $DESC: "
start-stop-daemon -d $DIR --start --quiet --pidfile $PID_FILE \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
kill -TERM `cat $PID_FILE`
rm $PID_FILE
echo "$NAME."
;;
restart)
echo -n "Restarting $DESC: "
kill -TERM `cat $PID_FILE`
rm $PID_FILE
sleep 1
start-stop-daemon -d $DIR --start --quiet --pidfile \
$PID_FILE --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload}" >&2
exit 1
;;
esac
exit 0
It's blowing up on the start-stop-daemon line, saying "start-stop-daemon: not found". But, when i plug the values into that line manually, and run it on the command line, it works.
它在 start-stop-daemon 行上炸了,说“start-stop-daemon: not found”。但是,当我手动将值插入该行并在命令行上运行它时,它可以工作。
My first thought was it was the shebang line but #! /bin/shshould be right shouldn't it? It's definitely the right folder and what i use in my other /etc/init.d scripts.
我的第一个想法是这是shebang线,但#! /bin/sh应该是对的,不是吗?这绝对是正确的文件夹,也是我在其他 /etc/init.d 脚本中使用的文件夹。
My second thought was that it's sudo related: i'd been testing start-stop-daemon in non-sudo and running /etc/init.d/ar_sendmail in sudo mode. But, i can run start-stop-daemon fine with sudo as well.
我的第二个想法是它与 sudo 相关:我一直在非 sudo 中测试 start-stop-daemon,并在 sudo 模式下运行 /etc/init.d/ar_sendmail。但是,我也可以使用 sudo 很好地运行 start-stop-daemon。
Kind of stumped, any ideas?
有点难住,有什么想法吗?
回答by sarnold
As @Dysaster points out, you're overwriting your PATHwith this line:
正如@Dysaster 指出的那样,你PATH用这一行覆盖了你的:
PATH=/var/lib/gems/1.8/bin
PATH=/var/lib/gems/1.8/bin
Because you're giving the complete pathname for your daemon, I think you probably don't even need to add /var/lib/gems/1.8/binto your path, unless ar_sendmailneeds to execute programs in that directory without knowing their path. (That would sure be unfortunate, but easily fixed with: PATH=/var/lib/gems/1.8/bin:$PATH.)
因为您为守护程序提供了完整的路径名,所以我认为您甚至可能不需要添加/var/lib/gems/1.8/bin到您的路径中,除非ar_sendmail需要在不知道路径的情况下执行该目录中的程序。(这肯定是不幸的,但很容易解决:PATH=/var/lib/gems/1.8/bin:$PATH。)
回答by Erik
Add a source /etc/profileto the start of the script, so you get your path setup.
将 a 添加source /etc/profile到脚本的开头,以便您设置路径。

