Table of contents
- 0. Preface
- 1. Write a script file to start the program
- 2. Set the startup application
- 3. Turn off the sudo permission to start the executable program
- 4. Whether the verification is successful
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
- 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
/path/to/your_project
is your executable program path, if your executable program requires sudo permission, you can change it tosudo /path/to/your_project
, in short, you can/path/to/your_project
replace with any command you want to execute in the terminal.- Save and close the file.
- Give the script executable permissions:
chmod +x startup_script.sh
2. Set the startup application
- Open the Startup Applications Preferences. You can open a terminal by pressing the key
Ctrl + Alt + T
combination , then enter the following command to open it:
gnome-session-properties
-
In the Startup Applications dialog box, click the Add button.
-
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.sh
replace with your actual startup script path.
-
Click the "Add" button.
-
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/sudoers
the file to allow certain users to run certain commands as administrator without entering a password. This way, your autostart script can use sudo
the command without requiring a password. Follow the steps below:
- Open a terminal and enter the following command to edit
/etc/sudoers
the file :
sudo visudo
- Add the following line to the opened file,
your_username
replacing with your username:
your_username ALL=(ALL) NOPASSWD: /path/to/your_project
- Make sure to
/path/to/your_project
replace with your actual executable path.
In this way, your bash script /path/to/your_project
does 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
reboot
the restart, and the program starts after booting!