简体   繁体   English

如何使用另一个脚本重新启动 python 脚本?

[英]How to restart a python script using another?

('The First script' takes input from the user and 'the second script' notify the task.) (“第一个脚本”从用户那里获取输入,“第二个脚本”通知任务。)

I have been trying to restart a python script using another one but i couldn't succeed it after trying to do a few methods.我一直在尝试使用另一个脚本重新启动 python 脚本,但是在尝试了几种方法后我无法成功。 I developed a reminder, notify user when time previously set by the user has arrived, app works on Linux and it have 2 python script.我开发了一个提醒,当用户之前设置的时间到达时通知用户,应用程序在 Linux 上运行,它有 2 个 python 脚本。 First one is for taking input that given by the user to schedule a task.第一个是接受用户提供的输入来安排任务。 For example, "Call the boss at 12:30 pm".例如,“下午 12:30 打电话给老板”。 Then Linux is going to notify it at 12:30 pm.然后 Linux 将在下午 12:30 通知它。 The other one is checking the inputs and notify them when the time comes.另一个是检查输入并在时间到来时通知他们。

In first script, i am trying to restart the other script when the user give a new task because the script needs to read the new task to notify it.在第一个脚本中,我试图在用户给出新任务时重新启动另一个脚本,因为脚本需要读取新任务来通知它。 Also I want to terminate the first script when it ran the second script.我还想在运行第二个脚本时终止第一个脚本。 But the second script must still be working.但是第二个脚本必须仍然有效。 In first script, I tried these commands to do that:在第一个脚本中,我尝试了这些命令来做到这一点:

os.system(f"pkill -f {path2}")
os.system(f"python {path2}")

These aren't work.这些都行不通。

Also I want to run the second script at the startup of my os.另外我想在我的操作系统启动时运行第二个脚本。

Summary:概括:

1- I wanna restart a python script using another one and the first one should be terminated when the second one is run. 1-我想使用另一个脚本重新启动 python 脚本,并且在运行第二个脚本时应该终止第一个脚本。

2- I wanna run the second script at the startup of my os. 2-我想在我的操作系统启动时运行第二个脚本。

Repository about my reminder app is here .关于我的提醒应用程序的存储库在这里

About 1:关于1:

Assuming the name of the other script is 2.py (Changeable with the code below), this worked for me pretty well:假设另一个脚本的名称是 2.py(可使用下面的代码更改),这对我来说非常有效:

1.py:

import subprocess
import os
import time

OTHER_SCRIPT_NAME = "2.py"

process_outputs = subprocess.getoutput("ps aux | grep " + OTHER_SCRIPT_NAME) # Searching for the process running 2.py
wanted_process_info = process_outputs.split("\n")[0] # Getting the first line only
splitted_process_info = wanted_process_info.split(" ") # Splitting the string
splitted_process_info = [x for x in splitted_process_info if x != ''] # Removing empty items
pid = splitted_process_info[1] # PID is the secend item in the ps output
os.system("kill -9 " + str (pid)) # Killing the other process
exit()

time.sleep(1000) # Will not be called because exit() was called before

2.py:

import time

time.sleep(100)

About 2:关于2:

In linux, you can execute scripts on startup by writing it into the /etc/rc.local file在 linux 中,您可以通过将脚本写入/etc/rc.local文件来在启动时执行脚本

Just run your scripts from the rc.local file and you are good to go:只需从 rc.local 文件运行您的脚本,您就可以使用 go:

/etc/rc.local:

python '/path/to/your/scripts'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM