如何在运行python脚本时隐藏终端窗口?

时间:2020-10-11 14:08:42

标签: python macos terminal background-process automator

我正在运行此代码以打开Goog​​le Chrome,并向我发送有关某个网站上任何更新的通知。我一直在从https://github.com/iholdroyd/oddsmonkey_eachway_calculator_automation/blob/master/script.py

使用此开源代码

我正在运行MacOS Mojave v 10.14.3。

我希望代码在启动/登录时运行,我认为可以从“系统偏好设置”>“用户和组”>“登录项”中执行。 我希望该过程在后台运行,并在网站上有任何更新时提醒我,即我不希望任务栏上有任何打开的窗口或其他图标。

我添加了以下代码行以使chrome窗口不可见并在后台运行:

options.add_argument("headless")

这似乎可行,因为我仍在收到通知,但没有打开的Chrome窗口。但是,当我运行脚本时,仍然有一个打开的python启动器窗口和两个终端窗口。为了运行脚本(保存为.py文件),我将其设置为使用Python启动器始终打开-我不确定是否有更好的方法来减少打开然后打开的窗口数量,例如直接通过终端。

当我尝试关闭终端窗口时,我(可以理解)收到以下错误消息:

Do you want to terminate running processes in this window?
Closing this window will terminate the running processes: Google Chrome, Google Chrome Helper (2), Google Chrome Helper (Renderer), chromedriver, Python, Google Chrome Helper (GPU).

我不想终止正在运行的进程,只希望它们在后台运行。无论如何,我是否可以运行代码而不必在桌面上打开终端窗口或在任务栏中将其最小化?我已经尝试过使用automator,但是我不确定该怎么做。

我以前从未做过任何编码,所以如果您能让我知道如何做,那就太好了。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以为此使用systemd单位文件。

单位文件格式

Future apiCall() async {
    var body =
        jsonEncode({"filepath": "patient/reports/1602333458533-Liver.jpg"});
    try {
      await http
          .post('http://3.6.197.52:3100/downloadFile',
              headers: {"Content-Type": "application/json"}, body: body)
          .then((http.Response response) => print(response.statusCode));
    } catch (e) {
      print(e);
    }
  }

将此文件命名为: your_file.service

将此文件放置在文件夹中: / etc / systemd / system

运行此命令以生效: systemctl daemon-reload

运行此命令以启动脚本: systemctl start your_file.service

运行此命令以在启动时运行脚本: systemctl enable your_file.service

运行此命令以检查脚本的状态: systemctl启动your_file.service *

运行此命令以停止脚本: systemctl stop your_file.service

运行此命令以从启动中删除脚本: systemctl disable your_file.service

相关问题