简体   繁体   English

设置为在 Mac 上启动时运行的 Python 脚本不会保持运行

[英]Python script set to run on boot on Mac will not stay running

I have a Python script I want to run at startup and remain running continuously on a Mac.我有一个 Python 脚本,我想在启动时运行并在 Mac 上持续运行。 I believe the script is executing but it does not remain running.我相信脚本正在执行,但它不会继续运行。
The setup is a plist script loaded with launchctl to start a Python script on boot - which should remain running always.设置是一个加载了 launchctl 的 plist 脚本,用于在启动时启动 Python 脚本——它应该始终保持运行。
The (always running) Python script uses the 'schedule' module which should execute a function at a specific time. (始终运行的)Python 脚本使用“schedule”模块,它应该在特定时间执行一个函数。
How can I make this script run for as long as the computer is on?只要计算机处于开启状态,我怎样才能让这个脚本一直运行? Can I see it running somewhere?我能看到它在某处运行吗?

Specifically: If I run just the Python script manually in a terminal it runs continuously, and executes the function at the specified time with schedule module.具体来说:如果我只在终端中手动运行 Python 脚本,它会连续运行,并在指定时间使用 schedule 模块执行该函数。
If I have the plist script loading at startup the Python script is NOT executing the function at the specified time ALTHOUGH, if I change the Python script to execute the function ONE TIME the plist script will execute it on startup.如果我在启动时加载了 plist 脚本,则 Python 脚本不会在指定的时间执行该函数,但如果我将 Python 脚本更改为一次执行该函数,则 plist 脚本将在启动时执行它。
[In all code below change '/path/to' to your paths, and to your Mac's username, etc. if replicating.] [在下面的所有代码中,将“/path/to”更改为您的路径,以及您的 Mac 用户名等。
The plist script... plist 脚本...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs$
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>Run.The.FB.photoblast</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/python</string>
        <string>/path/to/python/script.py</string>
    </array>
    <key>StandardErrorPath</key>
    <string>/var/log/python_script.error</string>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>  

I save this file to我将此文件保存到

/Users/<user_name>/Library/LaunchAgents/FB_Photo_script_launcher.plist  

and run...并跑...

sudo launchctl load -w /Users/<user_name>/Library/LaunchAgents/FB_Photo_script_launcher.plist  

The Python script below should post a photo to a Facebook page at 1:00pm.下面的 Python 脚本应该在下午 1:00 将照片发布到 Facebook 页面。 (For my question the Python script can do anything measurable). (对于我的问题,Python 脚本可以做任何可衡量的事情)。 Because of the 'while True' at the bottom this script should always be running and waiting for 'schedule.run_pending()' to fire.由于底部的“while True”,此脚本应始终运行并等待“schedule.run_pending()”触发。

import os
import time
import glob
import random
import facebook
import schedule

#wait 13 seconds to give the computer time to wake up
time.sleep(13)

def job():
  allphotofiles = glob.glob('/users/<user_name>/documents/fbcal/photos/*.jpg')
  one_photo = allphotofiles[random.randint(0, len(allphotofiles)-1)]
  #one_photo = glob.glob(one_random + '/*.jpg')[0]

  d = {
      'Calg Big': 'FACEBOOK_PAGE_TOKEN'}
  for i in d:
      graph = facebook.GraphAPI(access_token=d[i], version='3.1')
      graph.put_photo(image=open(one_photo, "rb"))
      print(i)


  os.rename(one_photo, '/users/<user_name>/documents/fbcal/used/'+one_photo[43:])

# job()

schedule.every().day.at("13:00").do(job())

while True:
  schedule.run_pending()
  time.sleep(1)  

Thank you for reading this question.感谢您阅读这个问题。 My situation is very convoluted.我的情况非常复杂。 Maybe I need a whole other approach.也许我需要一种完全不同的方法。 I tried making an 'app' using Automator but that had several problems.我尝试使用 Automator 制作一个“应用程序”,但这有几个问题。 Automator's version of Python couldn't import the Facebook SDK, my computers fans would run at full speed when I started the app manually or on startup. Automator 的 Python 版本无法导入 Facebook SDK,当我手动启动应用程序或启动时,我的计算机粉丝会全速运行。 I couldn't figure out how to schedule it, and I already have the scheduling part taken care of in the Python script.我不知道如何安排它,而且我已经在 Python 脚本中处理了安排部分。 I simply need the Python script to run on boot and never ever stop running.我只需要 Python 脚本在启动时运行并且永远不会停止运行。 Is there another timer I can use to fire the script at the same time each day without using Automator?是否有另一个计时器可以在不使用 Automator 的情况下每天在同一时间触发脚本?

I just removed the scheduling inside the Python script and set up the function to fire when the script is called.我刚刚删除了 Python 脚本中的调度,并设置了在调用脚本时触发的函数。 Then created a cron job to run every day at 2pm.然后创建了一个每天下午 2 点运行的 cron 作业。

EDITOR=/usr/bin/nano crontab -e

0 14 * * * is everyday at 2pm 0 14 * * * 是每天下午 2 点

0 14 * * * /anaconda3/bin/python /Users/mycomputername/Documents/FBcal/s2cheduler.py >> ~/cron.log 2>&1

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

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