init.d脚本不会在发生错误时重新运行python脚本

时间:2018-10-01 13:26:27

标签: shell ubuntu sh

我是Shell脚本的新手,并且在重新运行脚本时遇到了一些问题。我的脚本位于/etc/init.d/receiver上,当我使用以下命令sudo systemctl start receiver时,出现以下错误,

Job for receiver.service failed because the control process exited with error code. See "systemctl status receiver.service" and "journalctl -xe" for details.

为了测试崩溃时的“重试”,我故意在我的python脚本中添加了一条被调用来使程序崩溃的行。该行是someError = 'error。否则,调用python脚本的shell脚本将毫无问题地运行,但是该init.d shell脚本的全部目的是在发生错误的情况下重新运行py脚本并在启动时启动。这是我的代码,

#!bin/sh

### BEGIN INIT INFO
# Provides:             Test_receiver.py
# Reqired-Start:        $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        3 4 5 6
# Default-Stop:         0
# Short-Description:    Rabbit consumer script Test_receiver.py
### END INIT INFO

set -e

# /etc/init.d/startup: start and stop the Test_receiver.py script
# no file /etc/default/startup for added config at this time

. /lib/lsb/init-functions

cd /home/cschoom/Security/src

run_receiver(){
   log_action_begin_msg "Starting Test_receiver.py"
   log_file=/home/cschuma1/otherfile.txt
   python Test_receiver.py 2>> $log_file
   retval=$?

   if [ $retval == "0" ]; then
      echo "success"
      log_action_cont_msg "No issues"
   else
      echo "failure" >> $log_file
      log_failure_msg "ERROR WITH TEST_RECEIVER `date`"
      sleep 5
      run_receiver
   fi
}

case "$1" in
start)
   run_receiver
   ;;
stop)
   echo "killing Test_receiver script"
   log_action_begin_msg "Killing Test_receiver.py"
   kill $(pgrep -f 'python Test_receiver.py')
   ;;
restart|force-reload)
   log_action_begin_msg "Reloading Peach_receiver.py"
   run_receiver
   ;;
*)
   echo "Usage /etc/init.d/receiver (start|stop|restart|force-reload)"
   exit 1
   ;;
esac

exit 0

`

我要做的只是运行Test_receiver.py,获取返回值,然后基于该返回值,重新运行run_receiver函数,该函数再次调用我的python脚本,或者以0返回码退出。这是一个Rabbitmq使用者脚本,它将无限循环运行,因此我想重新运行python脚本,因为故障可能不在于脚本,而在于被测试的脚本。 (我的python脚本在其他计算机上运行测试,例如nmap)。任何帮助,将不胜感激。谢谢

0 个答案:

没有答案