在Mac(终端)/ Windows(命令行)中自动执行Python脚本

时间:2020-01-29 09:49:45

标签: python python-3.x windows macos

我希望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进行了尝试,但不知道我们如何安排它。

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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

相关问题