How to set the boot self-starting program in ubuntu

Table of contents


0. Preface

When deploying a program to the edge, we always hope that we can start the program we wrote when the power is turned on. This blog is used to record how to execute a command or an executable program on ubuntu boot

1. Write a script file to start the program

  1. Create a new startup script file, for example startup_script.sh, and add the following to the file:
#!/bin/bash
sleep 5  # 延迟5秒
#打开新的终端窗口并执行命令
gnome-terminal -- /path/to/your_project
  1. /path/to/your_projectis your executable program path, if your executable program requires sudo permission, you can change it to sudo /path/to/your_project, in short, you can /path/to/your_projectreplace with any command you want to execute in the terminal.
  2. Save and close the file.
  3. Give the script executable permissions:
chmod +x startup_script.sh

2. Set the startup application

  1. Open the Startup Applications Preferences. You can open a terminal by pressing the key Ctrl + Alt + Tcombination , then enter the following command to open it:
gnome-session-properties
  1. In the Startup Applications dialog box, click the Add button.

  2. In the pop-up dialog box, enter the following information:

    • Name: Enter an appropriate name such as "My Startup".
    • Command: Enter the full path to your startup script, eg /path/to/startup_script.sh.

Make sure to /path/to/startup_script.shreplace with your actual startup script path.

  1. Click the "Add" button.

  2. If your program does not require sudo privileges, you can reboot the system and check whether your startup scripts and programs are automatically executed.

  • If your program requires sudo permission, then see the next step

3. Turn off the sudo permission to start the executable program

You can edit /etc/sudoersthe file to allow certain users to run certain commands as administrator without entering a password. This way, your autostart script can use sudothe command without requiring a password. Follow the steps below:

  • Open a terminal and enter the following command to edit /etc/sudoersthe file :
sudo visudo 
  • Add the following line to the opened file, your_usernamereplacing with your username:
your_username ALL=(ALL) NOPASSWD: /path/to/your_project
  • Make sure to /path/to/your_projectreplace with your actual executable path.

In this way, your bash script /path/to/your_projectdoes not require a password when using the sudo command to execute. If you have other commands to add, the steps are the same as above.

4. Whether the verification is successful

  • The command line executes rebootthe restart, and the program starts after booting!

I am a salted fish_, if the article is useful to you, please like and collect it!

Guess you like

Origin blog.csdn.net/qq_69194611/article/details/130804418