ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux – Init脚本留下了数以千计的失效进程

2019-08-15 04:52:25  阅读:244  来源: 互联网

标签:bash linux raspberry-pi init gnu-screen


我的问题是我有一个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,那么不受影响.

标签:bash,linux,raspberry-pi,init,gnu-screen
来源: https://codeday.me/bug/20190815/1661018.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有