In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to set up the boot auto-run script in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Method 1: modify / etc/rc.d/rc.local file
The / etc/rc.d/rc.local file will not be run until all the services of the Linux system have been started. So if you want your script to be run after booting, you can add your own script path to the file.
First, however, you need to make sure that you have permission to run this file.
$chmod + x / etc/rc.d/rc.local
To demonstrate, we created a script that, when executed, writes a file with specific information in the home directory.
$vim auto_run_script.sh #! / bin/bash date > > / home/alvin/output.txt hostname > > / home/alvin/output.txt
After saving and exiting, give it executable permissions:
$chmod + x auto_run_script.sh
Then we add the script to the last line of the / etc/rc.d/rc.local file:
$vim / etc/rc.d/rc.local / home/alvin/auto_run_script.sh
Next, we can try the effect. Just restart the system:
$sudo reboot
After restarting, you will see the result of the script execution in the home directory.
Method 2: use crontab
As we all know, crontab is a scheduled task under Linux. When the time reaches the time we set, it can automatically trigger some scripts to run.
We can set the scheduled task time ourselves, and then write the corresponding script. However, there is a special task, called @ reboot, which we can actually see directly from its literal meaning, which is to run a script automatically after a system restart.
So what script will it run? How do we set up this script? We can set it through crontab-e.
$crontab-e @ reboot / home/alvin/auto_run_script.sh
Then, just restart it. The effect of running is similar to that above.
Method 3: use systemd services
The two methods described above can be used on any Linux system. But this method is only applicable to systemd system. How to tell whether it is a systemd system or not? Simply run the ps aux command to see if the process with pid 1 is systemd.
To achieve this, we need to create a systemd startup service and place it in the / etc/systemd/system/ directory.
The systemd startup service we created is as follows. Notice that the suffix is .service, not .sh.
$vim auto_run_script.service [Unit] Description=Run a Custom Script at Startup After=default.target [Service] ExecStart=/home/alvin/auto_run_script.sh [Install] WantedBy=default.target
As you can see from the content of the service, we will eventually call the script / home/alvin/auto_run_script.sh.
Then, we put the script in the / etc/systemd/systerm/ directory, and then we run the following two commands to update the systemd configuration file and start the service.
$systemctl daemon-reload $systemctl enable auto_run_script.service
When everything is ready, we can restart the system.
$reboot above is how to set the boot auto-run script in the Linux shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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.