Skip to main content

5.2.5 Auto Start

Video: https://www.bilibili.com/video/BV1rm4y1E73q/?p=15

There are many ways to add startup programs to the Ubuntu system. This section provides two methods for reference.

Set up auto-start

  1. Creating a startup script Using any text editor, /etc/init.d create a new startup script in the directory named your_script_name. The following is a sample script for reference:
    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides: your_service_name
    # Required-Start: $all
    # Required-Stop:
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Start your_service_name at boot time
    # Description: Enable service provided by your_service_name
    ### END INIT INFO
    /path/to/your/program &
    exit 0
  2. Set the startup script to have executable permissions
    sudo chmod +x /etc/init.d/your_script_name
  3. Use the update-rc.d command to add the script to the system startup item
    sudo update-rc.d your_script_name defaults
  4. Enable auto-start using the systemctl command
    sudo systemctl enable your_script_name
  5. Restart the development board to verify whether the self-start service program is running normally
    root@ubuntu:~# systemctl status your_script_name.service 
    ● your_script_name.service - LSB: Start your_service_name at boot time
    Loaded: loaded (/etc/init.d/your_script_name; generated)
    Active: active (exited) since Wed 2023-04-19 15:01:12 CST; 57s ago
    Docs: man:systemd-sysv-generator(8)
    Process: 2768 ExecStart=/etc/init.d/your_script_name start (code=exited, status=0/SUCCESS)

Add to rc.local

rc.local is a system service that automatically executes scripts or commands at system startup. This service is automatically called when the system boots up and executes user-specified scripts or commands after the system boots up, allowing for custom configuration or operations at system startup.

In early Linux distributions, rc.local was the last service to be run by default during the system startup process. With the popularity of systemd, rc.local is considered a legacy system service.

This is achieved by sudo vim /etc/rc.local adding a startup command at the end of the file, for example:

#!/bin/bash -e
#
# rc.local
#re
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Insert what you need

exit 0