我的问题是我有一个init脚本,它会生成数以千计的已失效的bash进程.我的目标是在启动时在自己的屏幕会话中创建一个名为“fr24Feed”的程序.在网上查看示例后,我编写了这个init脚本.
PROG="fr24Feed"
PROG_PATH="/home/pi/fr24Feed"
PROG_ARGS="--fr24key=xxxxxx --bs-ip=127.0.0.1 --bs-port=30003"
PIDFILE="/var/run/fr24Feed.pid"
start() {
if [ -e $PIDFILE ]; then
## Program is running, exit with error.
echo "Error! $PROG is currently running!" 1>&2
echo "Already running"
exit 1
else
echo "Starting"
cd $PROG_PATH
sleep 2
sudo -u pi screen -S fr24Feed -d -m ./$PROG $PROG_ARGS 2>&1 > /dev/null &
echo "$PROG started"
touch $PIDFILE
fi
}
stop() {
if [ -e $PIDFILE ]; then
## Program is running, so stop it
echo "$PROG is running"
killall $PROG
rm -f $PIDFILE
echo "$PROG stopped"
else
## Program is not running, exit with error.
echo "Error! $PROG not started!" 1>&2
exit 1
fi
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
echo "This script must be run as root" 1>&2 >> /home/pi/fr24Feed.log
exit 1
fi
case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
**)
echo "Usage: $0 {start|stop|reload}" 1>&2
exit 1
;;
esac
exit 0
用ps -ax我看到了
2063 ? Ss 0:19 SCREEN -S fr24Feed -d -m ./fr24Feed --fr24key=xxxxxx --bs-ip=127.0.0.1 --bs-port=30003
2064 pts/4 Ssl+ 49:19 ./fr24Feed --fr24key=xxxxxx --bs-ip=127.0.0.1 --bs-port=30003
随后成千上万的条目如下
3073 pts/4 Z+ 0:00 [bash] <defunct>
3078 pts/4 Z+ 0:00 [bash] <defunct>
3083 pts/4 Z+ 0:00 [bash] <defunct>
3088 pts/4 Z+ 0:00 [bash] <defunct>
3092 pts/4 Z+ 0:00 [bash] <defunct>
因为他们都在pts / 4上我怀疑他们是由我的初始脚本生成的,但是没有看到我出错的地方.也许STDERR和STDOUT没有被正确地重定向到/ dev / null?
解决方法:
Perhaps STDERR and STDOUT are not being correctly redirected to /dev/null?
正确. 2>& 1> / dev / null做的是将2重定向到与1相同的地方,即控制(伪)终端,并将1重定向到/ dev / null:
» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"'
Testing stdout
Testing stderr
» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 1> /dev/null
Testing stderr
» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 2> /dev/null
Testing stdout
» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' 2>&1 > /dev/null
Testing stderr
这样做的简单方法是使用&>:
» perl -e 'print "Testing stdout\n"; print STDERR "Testing stderr\n"' &> /dev/null
[no output]
请参阅example 3.6 here.请注意,尽管它表示重定向“每个输出”,但这指的是执行的shell,因此如果执行的进程使用其他文件描述符进行日志记录,或者重定向其自己的stdout / stderr以便它们永远不会到达shell,那么不受影响.