Preface Hello everyone, I am Liang Xu. At work, we often have a requirement to automatically start a script or service after the system starts. In Windows, we have many ways to set up startup, but how do we do it in Linux system? It is also possible to set the startup under Linux, but we may need to type some commands (there may also be a setting method in the UI interface, but I am not familiar with it, I am more into playing with commands). Below we introduce three simple but feasible methods of startup settings. Method 1: Modify the /etc/rc.d/rc.local file The /etc/rc.d/rc.local file will be run after all services of the Linux system are started. So if you want your own script to be run after booting, you can add your own script path to this file. However, first you need to make sure you have permission to run this file. $ chmod +x /etc/rc.d/rc.local For demonstration purposes, we have created a script that, when executed, will write 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 out the effect. Just restart the system: $ sudo reboot After restarting, you will see the results of the script execution in the home directory. Method 2: Using crontab As we all know, crontab is a scheduled task under Linux. When the time reaches the time we set, it can automatically trigger the execution of certain scripts. We can set the scheduled task time ourselves and then write the corresponding script. However, there is a special task called @reboot. We can actually see from its literal meaning that this task is to automatically run a script after the system restarts. 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 reboot. The running effect is similar to the above. Method 3: Using systemd service The two methods described above can be used on any Linux system. But this method is only applicable to systemd systems. How to distinguish whether it is a systemd system? It's very simple. Just 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. Please note that the suffix is .service instead of .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 From the content of the service, we can see that we will eventually call the script /home/alvin/auto_run_script.sh. Then, we place this script in the /etc/systemd/systerm/ directory, and then run the following two commands to update the systemd configuration file and start the service. $ systemctl daemon-reload $ systemctl enable auto_run_script.service After everything is ready, we can restart the system. $ reboot Summarize This is the end of this article about setting up Linux system to automatically run scripts at startup. For more relevant Linux startup scripts, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue implements paging function
>>: A brief understanding of the difference between MySQL union all and union
Enter the mysql command: mysql -u+(user name) -p+...
When faced with a SQL statement that is not optim...
Table of contents fold (reduce) Using for...of Us...
This article shares the specific code for canvas ...
Table of contents Abstraction and reuse Serial Se...
In Linux, the commands cat, more, and less can al...
Operating system: Win7 64-bit Ultimate Edition My...
Notice! ! ! select * from user where uid not in (...
Table of contents 1. Installation 2.APi 3. react-...
Official Website https://cli.vuejs.org/en/guide/ ...
1 Background JDK1.8-u181 and Tomcat8.5.53 were in...
In order to prevent non-compliant data from enter...
The reuse of code in vue provides us with mixnis....
1. Create a new user: 1. Execute SQL statement to...
Background Threads •Master Thread The core backgr...