I want to run a simple python script like:
import webbrowser
webbrowser.open('http://google.com/', new=2)
when the startup progress of my RPI 4 is finished. I tried something like "[How to run a script after bootup on RPi 4/Raspbian 10 (buster)"
I don't know why but it won't work for me if I change the ExecStart line into
ExecStart=python /home/pi/Desktop/startupBrowser.py
Did I miss anything?
2 Answers 2
As you wrote in a comment you only want to start the web browser. Wrap it into a python script isn't needed. That's only an additional layer of execution and waste of resources. Just start the browser direct. Because we need a graphical environment try this service:
rpi ~$ sudo systemctl --force --full edit run-browser.service
In the empty editor insert these statements, save them and quit the editor:
[Unit]
Description=Run web browser
After=graphical.target
[Service]
User=pi
WorkingDirectory=/home/pi
Environment=DISPLAY=:0
ExecStart=/usr/bin/chromium-browser http://google.com
[Install]
WantedBy=graphical.target
Enable the new service and reboot:
rpi ~$ sudo systemctl enable run-browser.service
rpi ~$ sudo systemctl reboot
If you want to run a python script then first you should use python3 instead of python. The latter uses deprecated python2 and has no support since 2020年01月01日 anymore. Then make sure that the script is running on the commandline with user pi. If it works then you can change ExecStart to something like:
ExecStart=/usr/bin/python3 /home/pi/my-script.py
-
Thank you this is working for me! But I want to open it with a python script because I want to simulate a keyboard press also in the same python script with the keyboard module. I think smth is wrong with my ExecStart command. Cause when I edit this line in your file it is not working againSoam.P– Soam.P2020年03月19日 19:05:02 +00:00Commented Mar 19, 2020 at 19:05
-
@Soam.P I have updated the answer with the last paragraph. It would be nice if you could accept the answer with a click on the tick on its left side. It would help me much for later references to a working browser start :-)Ingo– Ingo2020年03月19日 21:28:44 +00:00Commented Mar 19, 2020 at 21:28
-
Okay now when I test the file by entering "sudo sevice run-browser start" it works. But when I rstart the Pi the Browser is not starting automatically. DId you try to rebuild my issue?Soam.P– Soam.P2020年03月21日 14:26:27 +00:00Commented Mar 21, 2020 at 14:26
-
and yes I enabled it an got the outpu: Created symlink /etc/systemd/system/graphical.target.wants/run-rowser.service → /etc/systemd/system/un-browser.service.Soam.P– Soam.P2020年03月21日 15:07:04 +00:00Commented Mar 21, 2020 at 15:07
-
@Soam.P I would like to have a look at it but before please accept my answer :-)Ingo– Ingo2020年03月21日 17:17:49 +00:00Commented Mar 21, 2020 at 17:17
You are not allowed to use commands in systemd
services.:
The command to execute must be an absolute path name. Source: systemd.service
You should add the exact path of the command. You can find that by this command:
which python
You would get the output as it:
/usr/bin/python
Finally, use the output of which
command to your systemd
service.
It should be something like this:
ExecStart=/usr/bin/python /home/pi/Desktop/startupBrowser.py
How to simply create a systemd
service?
Create a file:
nano /etc/systemd/system/startupbrowser.service
Put all lines below there:
[Unit]
Description=startupbrowser service
[Service]
ExecStart=/usr/bin/python /home/pi/Desktop/startupBrowser.py
StandardOutput=syslog
StandardError=syslog
Restart=on-failure
User=root
Group=root
SyslogIdentifier=startupbrowser
[Install]
WantedBy=multi-user.target
Save the file and reload the daemon:
sudo systemctl daemon-reload
Test that is your code is running:
sudo service startupbrowser start
Check the log by:
journalctl -f -u startupbrowser.service
It will give you the real-time log of your python code to troubleshoot what's going on.
If everything was good as you intended, run this command to enable is as a startup service:
sudo systemctl enable startupbrowser.service
-
1Thanks for your answer but still doesn't work for me. I've saved the file and enabled it with "sudo systemctl enable filename.service" but it still doesn't start after booting. Do I have to execute additional commands?Soam.P– Soam.P2020年03月18日 18:00:17 +00:00Commented Mar 18, 2020 at 18:00
-
2@Soam.P You should run
sudo systemctl daemon-reload
to reload daemon because you have changed a service state. After that, you would be able to enable it as a startup service. Note that you have to create your service file with.service
at the end to the/etc/systemd/system
. Something like/etc/systemd/system/startupBrowser.service
. -- In addition, please avoid informing that it doesn't work, give us the reason, I mean, comment the output of the commands you have run.Mohi Rostami– Mohi Rostami2020年03月18日 18:28:59 +00:00Commented Mar 18, 2020 at 18:28 -
1Maybe I am doing smth completely wrong here is what I do: sudo systemctl --force --full edit startupBrowser.service | typing in your completed service file example, save it via Crtl+x | sudo systemctl daemon-reload | sudo systemctl enable startupBrowser.service | output: Created symlink /etc/systemd/system/multi-user.target.wants/startupBrowser.service → /etc/systemd/system/startupBrowser.service. | sudo reboot ---- seems like I still miss smth :/Soam.P– Soam.P2020年03月18日 22:45:20 +00:00Commented Mar 18, 2020 at 22:45
-
@Soam.P I answered your comment by editing the post. Check it out.Mohi Rostami– Mohi Rostami2020年03月19日 14:18:48 +00:00Commented Mar 19, 2020 at 14:18
-
okay because "Ingo's" example is working I think smth has to be wrong with the ExecStart line. Also my output at the log seems to be fine : -- Logs begin at Wed 2020年03月18日 23:43:27 CET. --Mär 19 19:47:35 raspberrypi systemd[1]: Started startupbrowser service.Mär 19 19:47:35 raspberrypi systemd[1]: startupbrowser.service: Succeeded.Soam.P– Soam.P2020年03月19日 19:07:57 +00:00Commented Mar 19, 2020 at 19:07
/usr/bin/python
.