I think there are multiple things one could do to achieve this, but am uncertain which way to go:
- if you use
systemd
create a systemd service I guess? How to create systemd service that runs a (python) script on startup? - some files similar to
.bashrc
always run on system startup/boot?; use this if you are less fancy / have no systemd
If it’s a service, best to use systemd or your OSs equivalent. If that’s not possible, the @boot cronjob was already mentioned.
Also maybe look at supervisord or s6.
If you want to be lazy, use crontab @reboot
Making a systemd unit is the “correct” way
Yeah after searching on the arch wiki, I guess systemd timers, not services, are specifically what I was interested in, thanks!
What do you want to accomplish? Systemd can do lots of things like monitoring, sandboxing and dependency management along with just running commands.
If you are on a Busybox init based system like OpenWRT you can also just write a init script.
My first simple use case: I want to start a http (
busybox httpd
) server on localhost inside a certain directory whenever I turn the PC on.Don’t use a script for this
I would setup Lighttpd since it is a proper server while being pretty lightweight. It should come with the needed systemd unit so that you can just do
sudo systemctl start lighttpd
Avoid writing scripts like the plague. Config files are much better and will scale better.
meta: I know such questions have been asked before on stackoverflow; but lets bring them to fediverse as well (I hate stackoverflow web-design, google pop-ups, and javascript). I noticed there are always multiple ways to do such things in Linux. I think some options are very popular, while others go unmentioned (by LLMs for example). Also on different linux distributions there are different options available as far as I understand.
In general, the kernel starts one program after it finishes booting, and that process starts everything else; traditionally it’s called ‘init’. systemd is a “modern” replacement.
Different distributions and unix variants use different “init” patterns and tooling; so find out what your distribution is using.
Those patterns are designed for this use case but you need to understand the different phases of startup and where your script should fit in.
What distro are you using?