Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does linux execute commands or scripts on restart or startup

Shulou Source: shulou.com Published: 2022-06-01 05:46:01 10月02日 Update

This article mainly shows you "how linux executes commands or scripts when restarting or starting". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to execute commands or scripts when linux is restarted or started".

Method 1: how to use the / etc/rc.d/rc.local file to run scripts or commands at restart or startup

Traditionally, the / etc/rc.local file is executed after all normal computer services are started at the end of the process of switching to the multi-user runlevel.

This method is also applicable to systemd systems.

You need to add your script location to the / etc/rc.d/rc.local file to run at startup.

Make sure the file has permission to run:

# chmod + x / etc/rc.d/rc.local

As a demonstration, we will create a simple example script. You can create any script you need.

# vi / opt/scripts/run-script-on-boot.sh #! / bin/bashdate > / root/on-boot-output.txthostname > / root/on-boot-output.txt

After the script completes, set the executable permissions:

# chmod + x / opt/scripts/run-script-on-boot.sh

Finally, add the script to the bottom of the file:

# vi / etc/rc.d/rc.local / opt/scripts/run-script-on-boot.sh

Restart the system to check:

# reboot method 2: how to use crontab to execute commands or scripts on restart or startup

Cron automatically executes scheduled jobs in the background at a specific time. You can do this with the special string @ reboot in the cron task. Reboot is a special string that allows the user to run any command or script at startup.

This example runs the / opt/scripts/run-script-on-boot.sh file when the system is restarted. We will use the same script as above.

To do this, simply add the following entry to the crontab file:

# crontab-e @ reboot / opt/scripts/run-script-on-boot.sh

Restart the system to check:

# reboot method 3: how to use the systemd service unit to run commands or scripts at restart or startup

This method applies only to systemd systems. The method is very simple.

We will demonstrate using the same script above.

To do this, you need to create a systemd startup script and place it in the / etc/systemd/system/ directory.

This is our example systemd startup unit script:

# vi sample-on-boot-script.service [Unit] Description=Run a Custom Script at StartupAfter=default.target [Service] ExecStart=/opt/scripts/run-script-on-boot.sh [Install] WantedBy=default.target

After placing the unit script in the systemd location, run the following command to update the systemd configuration file and enable the service:

# systemctl daemon-reload# systemctl enable sample-on-boot-script.service

Restart the system to check:

# additional hints for reboot

If you want to run the script in the background, you need to add the & symbol at the end

/ Path/To/My_Script &

If you want to run commands as different users, use the following format:

Su-$USER-c / Path/To/My_Script is all the contents of the article "how to execute a command or script when linux is restarted or started". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Script run command file system method content unit user example article service check special same for this location background character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL vpn Xiaomi Linux