在后台运行python程序

2024-05-15 16:11:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个Python程序,它可以检查产品价格,并在价格下降时向我发送电子邮件。它每天检查一次价格,我想让这个程序在后台运行。最好的方法是什么?你知道吗

我看过很多关于在命令后面加&的东西,比如:python myscript &,但是我想知道如果这样做并关闭我的计算机会发生什么。当我启动电脑时,它会重新开始运行吗?当然,一旦它开始运行,该如何阻止它。我用的是苹果笔记本电脑。你知道吗

提前谢谢。你知道吗


Tags: 方法命令程序苹果电子邮件计算机价格后台
0条回答
网友
1楼 · 发布于 2024-05-15 16:11:52

在macOS上设置循环作业的首选方法是使用启动代理。你知道吗

  1. ~/Library/LaunchAgents/中创建启动代理属性列表文件

    例如:seancedric.myscript.plist

  2. plist文件应该如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>[label for your launch agent. ex: seancedric.myscript]</string>

    <key>ProgramArguments</key>
    <array>
      <string>[absolute path to your script. ex: /Users/seancedric/bin/myscript]</string>
      <string>[any arguments, each as a <string> xml element]</string>
    </array>

    <key>Nice</key>
    <integer>1</integer>

    <key>StartInterval</key>
    <integer>[seconds: 60]</integer>

    <key>RunAtLoad</key>
    <true/>

    <key>StandardOutPath</key>
    <string>[absolute path to log file]</string>

    <key>StandardErrorPath</key>
    <string>[absolute path to log file]</string>
  </dict>
</plist>

有关更多信息,请参见创建启动守护程序和代理:

https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

网友
2楼 · 发布于 2024-05-15 16:11:52

我会做的有点不同。如果你打算把你的.py(或.pyw)文件转换为可执行文件(.exe),然后可以使用命令“pyinstaller-wyourfilename”执行此操作。这是一个youtube视频,有人在其中解释了它(https://www.youtube.com/watch?v=lOIJIk_maO4)。但我不知道这是否适用于mac

相关问题 更多 >