0

I'm running this code to open Google Chrome and to send me notifications for any updates on a certain website. I've been using this open source code from: https://github.com/iholdroyd/oddsmonkey_eachway_calculator_automation/blob/master/script.py

I'm running a MacOS Mojave v 10.14.3.

I want the code to run on startup/login, which I think that I can do from System Preferences>Users and Groups>Login Items. I want the process to operate in the background and just alert me when there are any updates on the website i.e. I don't want any open windows or additional icons on my taskbar.

I added the following line of code to make the the chrome window invisible and to operate in the background:

options.add_argument("headless")

This seems to work as I am still getting notifications but there is no open chrome window. However, when I run the script there is still an open python launcher window and two terminal windows. To run the script (saved as a .py file), I have set it to Always Open with Python Launcher - I'm not sure if there's a better way to do it which reduces the number of windows which then open e.g. directly through terminal.

When I try to close the terminal window I (understandably) get the following error message:

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).

I don't want to terminate the running processes, I just want them to operate in the background. Is there anyway that I can run the code without a terminal window having to be open on the desktop or minimised in the task bar? I've tried using automator but I'm not quite sure what to do.

I've never done any coding before so if you could let me know how to do this it would be great.

Thanks!

AcK
  • 2,063
  • 2
  • 20
  • 27
  • 1
    Try starting with `pythonw`: https://stackoverflow.com/questions/9705982/pythonw-exe-or-python-exe#:~:text=pythonw.exe%20is%20a%20GUI,is%20still%20running%20or%20not. – Mike67 Oct 11 '20 at 14:19

1 Answers1

0

you can use a systemd unit file for this.

unit file format

[Unit]
Description=your_description
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/bin/python2.7 scipt.py 
#your command you use to run script

[Install]
WantedBy=multi-user.target

name this file as : your_file.service

place this file in folder : /etc/systemd/system

Run this command to take effects : systemctl daemon-reload

Run this command to start your script : systemctl start your_file.service

Run this command to Run your script at startup : systemctl enable your_file.service

Run this command to check status of your script : systemctl start your_file.service*

Run this command to stop your script : systemctl stop your_file.service

Run this command to remove your script from startup : systemctl disable your_file.service

  • Thanks for this - I can't seem to find the folder /etc/systemd/system? – Luke Thomas Oct 11 '20 at 14:50
  • hey, i missed line about your OS. systemctl is not available in MAC. alternatively you can run your scipt with following command `nohup /path/to/test.py &` , after this you can close your terminal. to check if this process is running use `ps ax | grep test.py`. – sujit patel Oct 11 '20 at 15:10