3

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?

Soam.P
  • 57
  • 2
  • 6

2 Answers2

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
Ingo
  • 42,107
  • 20
  • 85
  • 197
  • 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 again – Soam.P Mar 19 '20 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 Mar 19 '20 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 Mar 21 '20 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 Mar 21 '20 at 15:07
  • @Soam.P I would like to have a look at it but before please accept my answer :-) – Ingo Mar 21 '20 at 17:17
  • @Soam.P Thank you :) What is this program sudo service?. I never used it before. Your comment with the symlink is hardly to read. Can you please make a new question to start your special script instead of only the browser? You need a different environment to run it. Comments are not the right place to discuss a different problem. They are too limited. – Ingo Mar 22 '20 at 11:28
  • the script I want to start is the script I wrote in my question. the sudo service command just starts the service manually – Soam.P Mar 22 '20 at 12:22
  • @Soam.P The script only starts the internet browser. It is not needed to wrap it into a python3 script. Then in addition to the browser, systemd has to start also the complete python3 interpreter only to start the browser and you have to have attention to its environment that is given on the command line but not on startup with systemd. This makes the trouble you have for nothing. – Ingo Mar 22 '20 at 13:57
1

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
M. Rostami
  • 4,323
  • 1
  • 17
  • 36
  • 1
    Thanks 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 Mar 18 '20 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. – M. Rostami Mar 18 '20 at 18:28
  • 1
    Maybe 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 Mar 18 '20 at 22:45
  • @Soam.P I answered your comment by editing the post. Check it out. – M. Rostami Mar 19 '20 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 Mar 19 '20 at 19:07
  • @Soam.P So, the service procedure has done. Check if your code has any error or functionality problem. Run it directly, etc. Cheers. – M. Rostami Mar 19 '20 at 21:27