How to run a script when the computer starts
Warning: I’m not going to talk about
- how to start a program when the user logs in a graphical desktop environment;
- the approach based on the configuration of a new systemd service.
That said, let's move on to the subject of this post: starting an executable (a script or a compiled program) at Linux startup. Basically there are two popular ways to do this job and depending on your distro in use, you probably won't have much choice.
Important: regardless of the solution you will use, always test it in depth, to make sure that everything works fine for both restarts and cold boots.
Solution 1. Editing /etc/rc.local (tested on Debian 10 and Ubuntu 16.04)
If your OS has this file, almost certainly you should use it. Typically it begins with “#!/bin/sh -e” and ends with “exit 0”. Insert the commands to execute in the middle of the file.
If the file does not exists, maybe you should use crontab (see below); anyway, if you are determined, you can try to create the file. And if it does not work... last chance: you can try to verify the state of the service and enable it using
systemctl status rc-local systemctl enable rc-local
Solution 2. Use crontab (tested on Ubuntu 18.04)
Modern crontab versions support startup script. Edit the contab scheduling file using
and add the following line
Posted on 2019-10-26
Copyright © 2019 Marcello Zaniboni