In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.