Auto Launch Program on Boot
This guide will show you how to launch a program on boot in a Linux system using systemd, routine execution is also covered.
Problem
You want to automatically launch a program whenever your linux machine boots up.
Background
In linux system boot sequence, BIOS/UEFI gets loaded and executed, later it triggers the boot loader (ex: GRUB) which loaded the OS. Once the OS is loaded, it execute a service called "systemd" [1]. This service later starts all of the background processes necessary for modern day OS. In our case, we can use the ability to launch processes/services powered by systemd to achive our goal.
Figure 1: Linux Boot Process [2]
Solution 1: systemd
Step 1: Create a Service File
In this step, let's create a service file that systemd can use to launch our program. After activated, the program launched by this service is considered as a daemon process.
- Create a service file in path
/lib/systemd/system/<service_name>.service
, which can very depending on the Linux distribution you are using. This is proven to work on both Ubuntu and Raspbian. - Open the file in a text editor and add the following content:
[Unit] Description=This is a test service for demonstration After=multi-user.target # When the service should be started StartLimitIntervalSec=0 # Interval to wait before restarting a service when it fails (return code is non-zero) [Service] User=<your_account_name> # Which account to run the service under, if not specified, it will run under root Type=simple # Type of service, simple is the most common option for one-off tasks KillMode=mixed # How to kill the service, mixed is used to kill all associated processes (including child processes) #TimeoutSec=60 # How long to wait before considering the service as failed and killing it, now disabled, set to 60 seconds #ExecStartPre=/bin/sleep 5 # Command to run before the main command, now disabled, set to sleep for 5 seconds ExecStart=/usr/bin/python3 /home/test.py # Command to run, in this case, it is a python script [Install] WantedBy=multi-user.target # Which case the service should be started, in this case, all account will trigger the service
If you want to know more about the options in the service file, you can search for them in various Linux documentation websites.
Step 2: Activate the Service
- Make systemd reload the service files.
- Enable the service to start on boot. Change following command to your service name.
- Start the service now.
- Check the status of the service.
- (optional) Check the log of the service.
- (optional) Stop the service.
- (optional) Disable the service from starting on boot.
Step 3: (optional) Routine Execution
In this step, let's create a timer service that will routinely trigger our service, which in turn will launch our program.
- Create a service file in path
/lib/systemd/system/<service_name>.timer
, which can very depending on the Linux distribution you are using. This is proven to work on both Ubuntu and Raspbian. - Open the file in a text editor and add the following content:
[Unit] Description=This is a test service for demonstration [Service] OnCalendar=hourly # How often to trigger the service, in this case, hourly #OnBootSec=1m # How long to wait before triggering the service after boot, now disabled, set to 1 minute #OnUnitActiveSec=1m # How long to wait before re-triggering the service after previous launch, now disabled, set to 1 minute Unit=test_service.service # Which service to trigger RemainAfterElapse=no # Whether to keep this timer service active after it triggers the target service [Install] WantedBy=timers.target # Which case the service should be started, in this case, by timers.target
- Follow the same steps as in Step 2 to activate the timer service.
Step 4: (optional) Write a Script to Automatically Finish All the Steps
You can modify the following script to your needs while following the Provided License.
- schedule_routine.sh: This is another script that will setup a
update_task.timer
to routinely trigger theupdate_task.sh
to execute a custom scriptupdate_task.sh
after system boot.
Solution 2: Desktop Entry
This solution is preferred for GUI applications, as it will launch the application in the user's desktop environment. Also, it is a lot easier to setup compared to systemd.
Step 1: Create a Desktop Entry File
- Create a desktop entry file in path
$HOME/.config/autosart/<app_name>.desktop
: - Open the file in a text editor and add the following content:
- Create the script that you want to launch in path
/opt/boot_trigger.sh
:
Next time you boot up your system, you will see a terminal window pop up automatically.
Step 2: (optional) Write a Script to Automatically Finish the Step
You can modify the following script to your needs while following the Provided License.
- schedule_startup.sh: This is a script that will setup a
update_task.service
to execute a custom scriptupdate_task.sh
after system boot.
References
- https://systemd.io/
- YouTube - ByteByteGo - How Does Linux Boot Process Work?
- https://unix.stackexchange.com/questions/48203/run-script-once-a-day-with-systemd
- https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html
- https://www.reddit.com/r/Fedora/comments/o1s94a/systemdissue_launch_gnome_terminal_startup/
- https://unix.stackexchange.com/questions/319341/how-do-i-debug-a-desktop-file
Error Correction
If you find any mistakes in the document, please create an Issue or a Pull request or leave a message in Discussions or send me a mail directly with the mail icon at the bottom right. Thank you!