【问题标题】:Automating Python script execution in Mac(Terminal) /Windows(Command line)在 Mac(终端)/Windows(命令行)中自动执行 Python 脚本
【发布时间】:2020-05-14 19:03:11
【问题描述】:

我希望 Python 程序使用终端,使用 mac 系统按计划或定期执行,但 Windows 也可以接受解决方案。

以下是我想要实现的步骤。

step 1) Open terminal and enter : sudo start --init**
        it will prompt to enter user name: usr_name
        password '-------------'

       step 2) after that run this command

        function params1 --params2 “value”

        step 3) After 3 to 4 minutes run the same program
          , It will ask for killing the previous session and will ask to enter users master password.

        or if we can do it in the same terminal press control+C 
, it will end the current session and bring the terminal to next line 
, then again the command function params1 --params2 “value”
,If we are doing this there will be no need to enter the user name and password as in step 1

以下脚本不起作用。在命令行中运行良好,但需要自动化和调度。调度是问题所在。

请提供任何帮助或建议。

import sys
import subprocess
import os
import openpyn
#sys.path.insert(1, ‘/Users/klr/Downloads/nordvpnipchange/openpyn/openpyn.py’)
#subprocess.check_output([‘ls’,‘-l’])
#subprocess.check_output([‘sudo openpyn --init’])
os.system(“sudo openpyn --init”)
#print(os.system(“sudo openpyn --init”))
#print (subprocess.check_output([“sudo openpyn --init”]))

第二个,

import subprocess
import os
subprocess.call(["openpyn DE --area “Berlin"], shell=True)
os.system("openpyn DE --area “Berlin")

在Mac中用Automator试了一下,如下图,但是不知道怎么安排。

【问题讨论】:

  • 到目前为止你有什么尝试?
  • @Marc 尝试使用 Python 使用 subprocess 和 os.system 将语句作为命令行参数传递,但没有成功。也尝试了这个链接github.com/jotyGill/openpyn-nordvpn,Using这个已经更新了包,如果我们手动尝试它将在终端窗口中工作。
  • @Marc 编辑了我的问题。你能帮忙吗?

标签: python python-3.x windows macos


【解决方案1】:

尝试使用Cron 作业。 Here's a guide on how to get started.

Cron 作业在调度脚本运行和自动化方面非常受欢迎。

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 2016-01-09
    • 1970-01-01
    • 2020-09-25
    • 2015-12-08
    • 2020-05-14
    • 2018-09-01
    • 2020-04-01
    • 2018-05-04
    相关资源
    最近更新 更多