WIN10系统python 自启动后台程序
时间: 2023-11-10 11:05:39 浏览: 28
如何设置?
可以按照以下步骤设置WIN10系统python自启动后台程序:
1. 打开“任务管理器”,切换到“启动”选项卡。
2. 点击“新增”,输入自启动程序的名称和程序路径。
3. 在程序路径后添加一个空格,再加上参数“-m your_module_name”,指定要运行的Python脚本。
例如,如果要自启动一个名为“test.py”的Python脚本,可以在程序路径后添加“-m test”,最终的命令应该是:python.exe -m test。
4. 单击“确定”即可保存设置,然后重新启动计算机,自启动程序就会在后台运行了。
相关问题
windos 如何使 python脚本在后台自动运行
有多种方法可以使Python脚本在后台自动运行,以下是其中的两种方法:
1. 使用Windows任务计划程序
Windows任务计划程序是Windows操作系统自带的一个工具,可以让用户在指定时间或事件触发时运行指定的程序或脚本。可以通过以下步骤将Python脚本添加到任务计划程序中:
1. 打开Windows任务计划程序,可以通过在开始菜单中搜索“任务计划程序”来打开;
2. 在任务计划程序中创建一个新任务,可以通过菜单栏中的“操作”->“创建任务”来创建;
3. 在创建任务的对话框中,设置任务的名称、描述、触发器、操作等参数。在操作参数中,选择“启动程序”并填写Python解释器的路径和要执行的脚本的路径;
4. 点击“确定”保存任务,并在任务计划程序中查看和管理任务。
2. 使用Windows服务
Windows服务是一种在后台运行的应用程序,可以在Windows操作系统中作为系统进程运行。可以通过以下步骤将Python脚本添加为Windows服务:
1. 安装pywin32模块,可以通过pip install pywin32命令安装;
2. 编写一个Python脚本,使用win32serviceutil模块将脚本注册为Windows服务。例如:
```python
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class PythonService(win32serviceutil.ServiceFramework):
_svc_name_ = 'PythonService'
_svc_display_name_ = 'Python Service'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
self.main()
def main(self):
# 在这里编写要执行的代码
pass
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PythonService)
```
3. 将Python脚本打包为可执行文件,可以使用pyinstaller等工具将Python脚本打包为exe文件;
4. 以管理员权限打开命令行窗口,使用sc create命令将Python脚本注册为Windows服务。例如:
```
sc create PythonService binPath= "D:\Python\PythonService.exe"
```
其中PythonService是服务的名称,D:\Python\PythonService.exe是可执行文件的路径;
5. 启动服务,可以在Windows服务列表中找到PythonService服务并启动。
以上是两种常用的方法,可以根据自己的需要选择适合自己的方法。
windows中python什么模块可以可以让程序变成后台守护程序
在 Windows 中,Python 可以使用 `pywin32` 模块将程序转换为后台守护进程。该模块提供了一种简单的方式来编写可重定向、可重启、可管理的后台守护进程。具体使用方法如下:
1. 安装 `pywin32` 模块:
```
pip install pywin32
```
2. 在 Python 脚本中引入 `win32serviceutil` 模块:
```
import win32serviceutil
```
3. 编写一个继承自 `win32serviceutil.ServiceFramework` 的类,并实现该类的 `SvcDoRun` 方法。该方法将在后台守护进程中运行:
```
import win32serviceutil
import win32service
import win32event
import time
class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = "MyService"
_svc_display_name_ = "My Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcDoRun(self):
while True:
print("My Service is running...")
time.sleep(1)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(MyService)
```
4. 使用以下命令将该 Python 脚本注册为 Windows 服务:
```
python my_service.py install
```
5. 使用以下命令启动 Windows 服务:
```
net start MyService
```
6. 使用以下命令停止 Windows 服务:
```
net stop MyService
```
注意:在 Windows 中使用 `pywin32` 模块将程序转换为后台守护进程时,需要确保代码中不会使用标准输入、标准输出和标准错误输出,否则可能会导致程序无法正常运行或输出无法预期。