ubuntu set self-start script

On Ubuntu 20.04, systemd is used to manage system services, but if you need to run some scripts at system startup, you can still use the autostart scripts in the init.d directory.

Here are the steps to set up an autostart script on Ubuntu 20.04:

1. Create script

First, you need to create an autostart script. You can create a script file using your favorite editor, and then add the commands you want to run in the file. For example, you could create a script called "my_script.sh" and add the following:

#!/bin/bash
echo "Hello World" > /tmp/my_script_output.txt

The above script will output "Hello World" to a temporary file when the system boots.

2. Copy the script to the /etc/init.d directory

Copy the script you just created to the /etc/init.d directory. You can copy the script to this directory with the following command:

sudo cp my_script.sh /etc/init.d/

3. Set script permissions

In order for the system to execute your script, you need to set the execution permission of the script. You can add execute permission to your script with the following command:

sudo chmod +x /etc/init.d/my_script.sh

4. Add startup script

Now, you need to tell the system to run your script on boot. You can use the update-rc.d command to accomplish this task. Run the following command to add your script:

sudo update-rc.d my_script.sh defaults

This command will create a symbolic link to the /etc/rc2.d directory to run your script on system boot. If you want to start the script in a different runlevel, modify the command accordingly.

5. Test the self-starting script

Now, you can reboot your system to test that the autostart script is working. After rebooting, you can check the /tmp directory for your script's output files to confirm that the script has been run on startup.

Summarize

By following the steps above, setting up autostart scripts on Ubuntu 20.04 should be fairly simple. Keep in mind that autostart scripts in the init.d directory are not the preferred method in Ubuntu 20.04, but it is still a viable method that can be used in some special cases. If you need more customization options, you can check out the systemd documentation on how to create more complex system services on Ubuntu 20.04.

Guess you like

Origin blog.csdn.net/qq_18376583/article/details/130447852