I thought that running commands on raspberry pi on boot was the easiest thing in my project. But it seems that it is complex enough not to work.
I've searched a lot on the internet on a simple objective that I couldn't solve yet. All I want to do is to launch chromium normally (not in full-screen mode or any other setting). I also need to start node.js server on boot.
I've followed this website but that didn't help at all.
I kept on seeing gnome-scheduler that is a GUI application that is supposed to run commands on boot. Now this package is deprecated, but I found previous versions at launchpad that I downloaded but I know nothing about how and where to put the downloaded files in order to work.
In the convenience of what I am looking for, I made a python script that is supposed to open a URL in chromium. And the issue should be simplified if I just put the two commands :
- Run Python (Open URL in Chromium)
- Start listening on Node.js server
But after using all the methods that were stated at the above tutorial none seem to work. Maybe the problem is that when the raspberry pi is booting other services doesn't seem to load completely.
OS: Raspbian Buster
The method I am working on is SYSTEMD:
import webbrowser
import time
time.sleep(30) #To debug if latency is the problem
webbrowser.open('https://www.google.com')
print("Hey There Log File! I made it till here!")
But this python script doesn't run because when I check the log file it is empty neither anything is printed nor the browser opens why?
Edit1:
Write a new Unit file sudo nano /lib/systemd/system/sample.service
Inserting code:
[Unit]
Description=My Sample Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/sample.py
[Install]
WantedBy=multi-user.target
In order to store the script’s text output in a log file, you can change the ExecStart line to:
ExecStart=/usr/bin/python3 /home/pi/sample.py > /home/pi/sample.log 2>&1
The permission on the unit file needs to be set to 644 :
sudo chmod 644 /lib/systemd/system/sample.service
Now the unit file has been defined we can tell systemd to start it during the boot sequence :
sudo systemctl daemon-reload
sudo systemctl enable sample.service
Then reboot Raspberry Pi.
Edit2:
Thanks to @JayBuckel and his question: Issues when trying to open Chromium at a certain URL on boot. I have made some progress. All I had to do was to insert my command @chromium-browser https://www.google.com
just before the @xscreensaver -no-splash
line and Chromium starts at boot and directs to the relevant URL.
All I have to do now is to run node server.js
command somewhere.
Edit3:
Currently, I am reading about how to Run node.js service with systemd?
Take a look at this for running a node server continously using pm2
https://raspberrypi.stackexchange.com/a/94850/90033
time.sleep(30) #To debug if latency is the problem
It will be if the program does not properly background itself; it will be killed at some point because of that.time.sleep(30)
after realizing that the code doesn't work. But it is a good point of view. Thanks.