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.
Related
I made an app in Thonny that when I run it, shows a camera connected to RPI and shows the image in an interface. You can also use the keyboard to do some things in the interface, take photos, etc.
I always execute it using thonny (run button), but now, I am trying to autostart the program in python when Raspbian ends its starting (after a shutdown or reboot).
I tried to do it in 3 ways:
autostart
lxde-pi/autostart
crontab
But it doesn't work. It starts Raspbian, but nothing more. Any ideas or code to do it?
Thanks a lot.
This might be a bit late for #mash_10, but for anyone else who lands here my solution that auto fires a bash script on the home folder. To add new automatic startups I just add code to my personal bash script. This works on Buster rpi.
Details are in my answer here How to automatically launch python file once GUI has loaded on Raspbian Pixel
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
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
I have really tried looking around but all the examples I find do not work for me.
I have a python script in a Raspberry Pi that starts using CRON....all good!
I would like to be able to debug the ouput that the script prints in screen from time to time so the "screen" command needs to be used to create a session and ensure that I can login and logoff using putty as needed.
I also add the "&" so it will run in background.
So currently I did:
1) Open cron
sudo crontab -e
2) Add the following line
#reboot python /home/pi/python/monitor.py &
This works correctly, I just need to know how to make a "screen" session open before launching the python script.
Thank you very much for your help!
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