Python 我的守护进程在启动后直接死亡

Python 我的守护进程在启动后直接死亡,python,linux,bash,service,daemon,Python,Linux,Bash,Service,Daemon,我必须用Python3开发一个程序,在特定时间自动执行程序。我必须使用守护进程,不能使用外部库 这就是为什么我创建了一个安装程序来配置遵循本教程的守护进程 文件gobatch是自动执行程序的程序 #! /usr/bin/python3 # -*- coding: utf8 -*- import os import sys import time import shutil # Check if script is start with root rights if os.geteuid()

我必须用Python3开发一个程序,在特定时间自动执行程序。我必须使用守护进程,不能使用外部库

这就是为什么我创建了一个安装程序来配置遵循本教程的守护进程

文件gobatch是自动执行程序的程序

#! /usr/bin/python3
# -*- coding: utf8 -*-

import os
import sys
import time
import shutil

# Check if script is start with root rights
if os.geteuid() != 0:
    exit('You need to have root privileges to run this script. Use \'sudo ./install.py\'.')

# Title
print('----- ProcessManager install ----- \n')

time.sleep(1)

# Get path of gobatch file
gobatchPath = input("Please enter the path (absolute) where the ProcessManager gobatch program is located: ")

# Check if file exists
try:
    with open(gobatchPath):
        pass
except IOError:
    exit('Error : file does not exists.')

# Copy gobatch file into /usr/bin/
shutil.copy(gobatchPath, '/usr/bin/gobatch')

# Create and fill the automatic launch program
description = 'Deamon that allows you to run cyclicaly at a date or a specific time a program'

fileContent = '#### BEGIN INIT INFO \n' \
              '# Provides:          chillispot et freeradius dans le chroot \n' \
              '# Required-Start:    $local_fs $network \n' \
              '# Required-Stop:     $local_fs $remote_fs _\n' \
              '# Default-Start:     2 3 4 5 \n' \
              '# Default-Stop:      0 1 6 \n' \
              '# Short-Description: Wireless & LAN Access Point Controller \n' \
              '# Description:       ChilliSpot is an open source captive portal \n' \
              '#                    or wireless LAN access point controller. \n' \
              '### END INIT INFO \n\n\n' \
              'DESC="' + description + '"\n' \
              'DEAMON=/usr/bin/gobatch'

file = open('/etc/init.d/gobatch', 'w')
file.write(fileContent)
file.close()

# Give the rights
os.chmod('/etc/init.d/gobatch', 0o755)

# Save gobatch file to be active
os.system('update-rc.d gobatch defaults')
但是,当我使用:/etc/init.d/gobatch start启动服务并显示状态时,我会获得服务gobatch status

你能告诉我我的方法是正确的还是有其他的解决方案吗

你知道为什么我的守护进程在启动后直接死亡吗

谢谢你的帮助

更新

目前,我的gobatch文件不是一个真正的程序,因为我更喜欢在之前创建deamon

gobatch文件

我的工作是创建守护程序的安装程序,而不是gobatch程序。必须由另一个人来做。这就是为什么我要使用一个最小的内容

更新2:

我的新/etc/init.d/gobatch:

使用此代码,我得到以下错误:

gobatch.service:在执行生成/etc/init.d/gobatch:EXEC格式错误的步骤中失败

更新3:

日志文件中的最后一个错误是:

詹夫。08 15:33:07 ubuntu systemd[1]:无法启动SYSV:ChilliSpot是一个开源的捕获门户


我希望在那之后,我的程序能正常工作。

您的脚本会立即响应并退出,因此它不是一个牧师。后台程序需要在调用后继续运行

您需要有一个行为类似于执事的程序来测试其安装。安装不会从常规程序中创建deamon。它只会为运行它准备环境

这个问题将为您提供更多关于如何用Python编写适当的守护进程的信息。

我怀疑,一旦gobatch打印,执行就完成了

试一试


你检查过系统日志文件了吗?不,我不知道在哪里可以看到。进入/var/log/syslog?问题似乎出在gobatch程序本身,而不是安装程序中。因此,发布程序源代码而不是安装程序会更有意义。@TomášCerha感谢您的回答。我更新了我的问题。至于Exec格式错误:我猜你漏掉了这一行/bin/bash/etc/init.d/gobashI的第一行需要使用while true这样的循环;例如,你知道它是否工作吗?是的,执事通常会在某种无限循环中运行。它用于学校项目,我不能使用库或现有代码。所以我不知道使用是不是一个好主意。对不起,我刚才注意到你问题的最后一句话,所以我有点困惑。我不知道你不在乎写执事本身。问题是您需要一个行为类似于执事的程序来测试其安装。安装不会从常规程序中创建deamon。它只会为运行它准备环境。我觉得您的init.d脚本文件内容不完整。它通常包含一个case语句,运行不同的start/status/stop/restart命令。我将此代码复制到我的gobatch文件中,启动守护进程并直接检查状态,问题是相同的。编写一个deamon并不是那么容易。这就是为什么我链接了一个问题来解释它,而不是试图在这里解释它。例如,deamon通常在后台运行并使用PID文件。
gobatch.service - SYSV: ChilliSpot is an open source captive portal
  Loaded: loaded (/etc/init.d/gobatch; bad; vendor preset: enabled)
  Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)
#! /bin/bash

echo "it works !"
#!/bin/sh
#### BEGIN INIT INFO 
# Provides:          chillispot et freeradius dans le chroot 
# Required-Start:    $local_fs $network 
# Required-Stop:     $local_fs $remote_fs _
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6 
# Short-Description: Wireless & LAN Access Point Controller 
# Description:       ChilliSpot is an open source captive portal 
#                    or wireless LAN access point controller. 
### END INIT INFO 


PATH=/bin:/usr/bin:/sbin:/usr/sbin 
DESC="Deamon that allows you to run cyclicaly at a date or a specific time a program" 
NAME=gobatch 
DEAMON=/usr/sbin/gobatch 
PIDFILE=/var/run/$NAME.pid 
SCRIPTNAME=/etc/init.d/"$NAME" 

. /lib/lsb/init-functions 

case "$1" in 
start) log_daemon_msg "Starting gobatch" 
       start_daemon -p $PIDFILE $DAEMON 
       log_end_msg $? 
       ;; 
stop) log_daemon_msg "Stopping gobatch" 
      killproc -p $PIDFILE $DAEMON 
      RETVAL=$? 
      [ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE 
      log_end_msg $RETVAL 
      ;; 
restart) log_daemon_msg "Restarting gobatch" 
         $0 stop 
         $0 start 
         ;; 
esac 
exit 0
#! /bin/bash
for i in $(seq 1 10)
do
    echo "it works !"
    sleep 1
done