我有一个简单的python脚本test.py
:
#!/user/bin/python
print "Why is it not redirecting?"
然后我有一个init.d脚本,我正在尝试./test.py &> logfile.log & PID=$!
,除了发生的事情是PID错误,它打印到我的shell而不是重定向。我正在测试脚本,方法是将它放在/etc/init.d并运行sudo service productcrawler-router start
(我正在使用sudo,因为我的真实脚本是在服务器上打开端口)。 init.d脚本的相关部分。
case "$1" in
start)
if [ -f $PIDF ]
then
echo "$NAME is currently running. Killing running process..."
$IEXE stop
fi
cd $LDIR
if [ -f $LDIR/$NAME.log ] ; then
echo "File already exist."
fi
sudo python ./$EXEC &> $LDIR/$NAME.log & MYPID=$!
ls -l $LDIR | grep $NAME
echo $MYPID
ps aux | grep "router"
ps aux | grep $MYPID
echo "$NAME are now running."
;;
输出:
-rw-r--r-- 1 root root 0 2011-04-25 13:57 productcrawler-router.log
11944
Why is it not redirecting?
root 11053 0.0 0.2 20364 5704 pts/2 Sl 13:45 0:00 python ./router.py
root 11933 2.0 0.0 1896 552 pts/2 S+ 13:57 0:00 /bin/sh /etc/init.d/productcrawler-router start
root 11948 0.0 0.0 4008 752 pts/2 S+ 13:57 0:00 grep router
root 11950 0.0 0.0 4008 756 pts/2 S+ 13:57 0:00 grep 11944
productcrawler-router are now running.
然后我有一个init.d脚本:
#! /bin/sh
# chkconfig 345 85 60
# description: startup script for produtcrawler-router
# processname: producrawler-router
NAME=productcrawler-router
LDIR=/etc/productcrawler/services
EXEC=test.py
PIDF=/var/run/productcrawler.pid
IEXE=/etc/init.d/productcrawler-router
### BEGIN INIT INFO
# Provides: productcrawler-router
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 5
# Default-Stop: 0 1 2 3 6
# Description: Starts the productcrawler-router service
### END INIT INFO
if [ ! -f $LDIR/$EXEC ]
then
echo "$LDIR/$EXEC not found."
exit
fi
case "$1" in
start)
if [ -f $PIDF ]
then
echo "$NAME is currently running. Killing running process..."
$IEXE stop
fi
cd $LDIR
if [ -f $LDIR/$NAME.log ] ; then
echo "File already exist."
fi
sudo python ./$EXEC &> $LDIR/$NAME.log & MYPID=$!
ls -l $LDIR | grep $NAME
echo $MYPID
ps aux | grep "router"
ps aux | grep $MYPID
echo "$NAME are now running."
;;
stop)
if [ -f $PIDF ]
then
echo "Stopping $NAME."
PID_2=`cat $PIDF`
if [ ! -z "`ps -f -p $PID_2 | grep -v grep | grep '$NAME'`" ]
then
kill -9 $PID_2
fi
rm -f $PIDF
else
echo "$NAME is not running, cannot stop it."
fi
;;
force-reload|restart)
$0 stop
$0 start
;;
*)
echo "Use: /etc/init.d/$NAME {start|stop|restart|force-reload}"
exit 1
esac
过去两个小时,我一直在撞墙。有人有任何想法吗?
答案 0 :(得分:1)
您不应在init脚本中使用sudo
。您不应在使用&>
的任何脚本中使用#!/bin/sh
。您可能不应该在init脚本中使用$!
,我认为您对它的使用确实是错误的。
我建议使用start-stop-daemon
使一切变得更简单,特别是PID的东西,但我不确定你是否可以将输出重定向到日志文件。