I have a script that I run with
python3 /path/script.py
I cannot figure out how to make the script run at startup.
Any pointers would be great!
I have a script that I run with
python3 /path/script.py
I cannot figure out how to make the script run at startup.
Any pointers would be great!
Run it as a service.
Create /etc/systemd/system/myscript.service:
[Unit]
Description=My Script
[Service]
ExecStart=/usr/bin/python3 /path/script.py
[Install]
WantedBy=multi-user.target
Then run it with:
sudo systemctl start myscript # Runs the script now
sudo systemctl enable myscript # Sets the script to run every boot
There are lots of other things you can do like make it run as a specific user with User=, Set it to run only after networking is available with After=networking.target, or lots of other stuff. If it launches a GUI, then you'll probably want to run it as a user-service.
See man systemd.unit and man systemd.service for more options.
myscript.service file in /lib/systemd/system/ directory. Otherwise, you will get an error Unit myscript.service not found
/lib/systemd/system is reserved for unit files deployed as part of packages. /etc/systemd/system is for local configuration (local units, overrides, and install relationships). systemd reads both /etc/systemd/system and /usr/systemd/system. On non-debian systems, you may find units in /usr/lib/systemd/system
There are multiple ways to do this based on your needs and conditions such as user privileges and the order of execution whether it should wait for other services to start or not. the easiest ones are :
crontab can be used like the following:
#crontab -e
@reboot python /path/your_script.py &
Or you can mention the the command in /etc/rc.local this on has detailed explanation here
Based on your needs to see logs or other details you can use other options here