无限地在后台运行Python脚本

我正在尝试编写一个在另一个服务器上运行的python脚本,即使我在我的PCterminal上closures了我的服务器连接,它也会继续在该服务器上运行。当脚本保持活动状态时,它会无限次地运行网站(UI),事件发生后,它会适当地启动某些泊坞窗并继续收听PosgreSQL事件。

当我尝试使用nohup (在后台运行脚本)时,它在后台运行,但无法收听任何事件。 有没有人从事过类似的事情? 请分享你的想法。 我正在分享我的脚本的一部分。

 self.pool = await asyncpg.create_pool(user='alg_user',password='algy',database='alg',host='brain',port=6543) async with self.pool.acquire() as conn: def enqueue_listener(*args): self.queue.put_nowait(args) await conn.add_listener('task_created', enqueue_listener) print("Added the listener") while True: print("---- Listening for new job ----") conn2, pid, channel, payload = await self.queue.get() x = re.sub("[^\w]", " ", payload).split() print(x) if x[5] == '1': tsk = 'TASK_ID=%s' % str(x[1]) if x[3] == '1': command = "docker run --rm -it -e ALGORITHM_ID=1 -e TASK_ID=%s --network project_default project/docked_prol:1.0" % (str(x[1])) subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) if x[3] == '8': command = "docker run --rm -it -e ALGORITHM_ID=8 -e TASK_ID=%s --network project_default project/docked_pro:1.0" % (str(x[1])) subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) 

在手动运行时,脚本运行得非常好,但只需要一些实现方法的build议。

在没有提供外部input的情况下,如果进程/脚本自动运行,只能通过nohup在后台运行某些内容,因为无法为后台进程提供手动input。 首先,尝试检查进程是否仍在后台运行(ps -fe | grep processname)。

如果它正在运行,那么检查'nohup.out'文件以查看进程卡住的位置。 这将在您开始进程的相同目录中生成。 这会给你一些想法,在这个过程中发生了什么。