How to hide terminal window whilst running a python script? - python

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!

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

Related

Executing code on server while shutting down the computer

I have Python code that I want to run on a GPU server. Running it is time-consuming and sometimes, I get disconnected from the Internet or server and I have to re-run it. So, I need to let it run and shut down my computer. Is there any way?
You can use screen to maintain a session and achieve the goal that allows you to run the code on the server.
Refer to this: https://www.gnu.org/software/screen/
Some basic commands:
Create session named RunWork
screen -S RunWork
List all sessions:
screen -ls
Open a session
screen -r SessionID
...
If it is a windows server create a bat file.
Run the python script and the shutdown command. You will have to be an admin to shutdown the computer from a script.
bat file
c:\python27\python.exe c:\somescript.py %*
shutdown /s /f /t 0
If you are Linux-based OS then Tmux can help you.
Tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.
https://github.com/tmux/tmux/wiki

How can I automatically run 2 python scripts at the same time in a virtual environment when booting up the Raspberry Pi?

Note: I'm new to everything so bare with me.
I'm using a RPi 4B with Buster. My goal is to automatically run 2 python scripts at the same time when the pi first boots up. Both scripts are in a virtual environment. The first script is called sensor.py which basically uses an ultrasonic distance sensor to continuously calculate distances between the sensor and an object. The other is an object recognition script from Tensorflow Lite called TFLite_detection_webcam.py that identifies objects from a camera feed. I can't use rc.local for autorunning because the object recognition script uses a picamera feed as an input, which rc.local doesn't support. So my preferred option is using autostart. I was able to successfully get the sensor.py script to autorun by issuing this in the terminal: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart and adding this to it: /home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/sensor.py. In this case, tflite1-env is the virtual environment being activated. However, I don't know how to get the second script to run. To run it regularly, I would issue the following into the terminal and the camera feed would pop up on the screen as a window.
cd tflite1
source tflite1-env/bin/activate
python3 TFLite_detection_webcam.py --modeldir=TFLite_model
I've tried to get this script to run by adding this to the autostart file: /home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/TFLite_detection_webcam.py --modeldir=TFLite_model but it doesn't seem to be working. I've tried to run it using shell files, but every time that I run a shell file in the autostart file such as adding ./launch.sh to the bottom, nothing happens. Any help getting the second script to run at the same time as the first upon startup would be greatly appreciated. Thanks in advance.
Use Systemd. Set up Systemd unit files in /etc/systemd/system, e.g.
kitkats-sensor.unit
[Unit]
After=network.target
[Service]
ExecStart=/home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/sensor.py
WorkingDirectory=/home/pi/tflite1/
User=pi
Group=pi
kitkats-tflite.unit
[Unit]
After=network.target
[Service]
ExecStart=/home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/TFLite_detection_webcam.py --modeldir=TFLite_model
WorkingDirectory=/home/pi/tflite1/
User=pi
Group=pi
Then enable the unit files with systemctl enable kitkats-tflite and systemctl enable kitkats-sensor (to have them autostart) and systemctl start kitkats-tflite (and sensor) to start them right away.
You can then see them in e.g. systemctl, and their logs are diverted to journalctl.

Linux - Open chromium from a script that runs on startup

I have a bash script that I've defined to run in startup, which runs a python script that waits for a command from another process, and when it gets it, it should open a chromium window with a certain URL.
When I run this script manually it works fine, but when the script runs from startup, I get an error (displayed in syslog):
Gtk: Can't open display
I guess that's because it's running in a startup mode so it doesn't actually have a display to "lean" on...
I was wondering if there's any way to get this work, anyway?
Thanks in advance
In your script that runs on startup try DISPLAY=:0 <command> &
To clarify DISPLAY=:0 simply sets which monitor your window opens on with 0 representing the first monitor of the local machine.

Executing python script without being connected to server

I need to execute python script on remote server (access through puTTY), but I don't have a stable Internet connection, and every time I execute the script I get problems after several minutes due to my Internet getting disconnected.
How do I remotely execute the script without being connected to server?
(e.g. I connect to server, run script, and can logout while executing)
You can use a Linux Screen, it opens a background terminal and keeps a shell active even through network disruptions.
Open the screen typing in your terminal $ screen and execute there your script, even if you lose connection it won't kill the process.
Here you will find a well explained How to for this program. I use it for my regular day working on remote.
try this
nohup your_script >/dev/null 2>&1 &
program will be running in background

How to continuously run a Python script on an EC2 server?

I've setup an Amazon EC2 server. I have a Python script that is supposed to download large amounts of data from the web onto the server. I can run the script from the terminal through ssh, however very often I loose the ssh connection. When I loose the connection, the script stops.
Is there a method where I tell the script to run from terminal and when I disconnect, the script is still running on the server?
You have a few options.
You can add your script to cron to be run regularly.
You can run your script manually, and detach+background it using nohup.
You can run a tool such as GNU Screen, and detach your terminal and log out, only to continue where you left off later. I use this a lot.
For example:
Log in to your machine, run: screen.
Start your script and either just close your terminal or properly detach your session with: Ctrl+A, D, D.
Disconnect from your terminal.
Reconnect at some later time, and run screen -rD. You should see your stuff just as you left it.
You can also add your script to /etc/rc.d/ to be invoked on book and always be running.
You can also use nohup to make your script run in the background or when you have disconnected from your session:
nohup script.py &
The & at the end of the command explicitly tells nohup to run your script in the background.
If it just a utility you run ad-hoc, not a service daemon of some kind, i would just run it in screen. Than you can disconnect if you want and open the terminal back up later... Or reconnect the terminal if you get disconnected. It should be in your linux distros package manager. Just search for screen
http://www.gnu.org/software/screen/
nohup runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.
Syntax:
nohup Command [Arg]...
Example:
nohup example.py
nohup rasa run
Also, you can run scripts continuously using the cron command.
For more:
https://ss64.com/bash/nohup.html
https://opensource.com/article/17/11/how-use-cron-linux

Categories

Resources