'Auto-run python script when system is booted on jetson nano
How to auto-run python script made by me when the system is booted on jetson nano?
Solution 1:[1]
Try StartupApplications
and add your python execution shell command with proper path.
An even better solution will be to use crontab
.
crontab -e
Add @reboot python path/to/script
so that the script gets executed every time you reboot.
This link might help you.
Solution 2:[2]
As an alternative to systemd or crontab, you can try pm2. It's very easy to configure and use. Just follow a quick start guide. Or just test it the following way:
pm2 start app.py
pm2 save
Note that you should initially generate a startup script to make it work on boot.
Solution 3:[3]
Step 1: Create a shell file.
sudo nano /usr/local/bin/startup.sh: Type this on the terminal. A new sh file is created. This file consists of the python file that is to be executed. I gave the name startup.sh. It can be any name XYZ.sh
#! /bin/sh: This is called the shebang. This would execute our script using a Bourne shell. This tells the system that the commands in this file should be fed to the interpreter.
sleep 10: This pauses a script for a certain amount of time. He re we pause it for 10 seconds.
In the next line, we insert the code that we use to run the program on the terminal. OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 path/of/the/python/code.py
It looks like this:
#! /bin/sh
sleep 10
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 /home/sooraj/Downloads/incabin-monitoring-system-main/netstreamfeb17.py
Now we close the file using Ctrl+X. and save it.
Step 2: Create a service file
sudo nano /etc/systemd/system/startup.service
Things to write inside it.
[Unit]
Description = INTU_IPC start-uo specific script
[Service]
Type= idle
ExecStartPre = /bin/sleep 10
ExecStart = /usr/local/bin/startup.sh
User=sooraj
# write your user name here
[Install]
WantedBy = multi-user.target
Now we close the file using Ctrl+X. and save it.
step 3: Give permission.
sudo chmod +x /usr/local/bin/startup.sh
step 4: enable, start and stop
sudo systemctl enable startup.service
sudo systemctl start startup.service
sudo systemctl stop.service
After starting, to view if it works, we can observe it in the terminal by
journalctl -u startup.service -f
If we edit the service file for the next time, we need to reload it before enabling and starting.
sudo systemctl daemon-reload
sudo systemctl enable startup.service
sudo systemctl start startup.service
Additional information.
systemctl is-enabled startup.service
#checks if the service file is enabled.
systemctl is-failed startup.service
#checks if the service file failed to start.
systemctl is-active startup.service
#checks if the service file is active.
sudo systemctl list-unit-files — type=service
#display the list of service files.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sharan |
Solution 2 | Sergey Korol |
Solution 3 | sooraj abraham |