首页 > 解决方案 > 在 Mac(终端)/Windows(命令行)中自动执行 Python 脚本

问题描述

我希望 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试了一下,如下图,但是不知道怎么安排。

在此处输入图像描述

标签: pythonpython-3.xwindowsmacos

解决方案


尝试使用Cron作业。这是有关如何开始的指南。

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


推荐阅读