Example of how to set up a Linux system to automatically run a script at startup

Example of how to set up a Linux system to automatically run a script at startup

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:
  • Three ways to set up Linux services to start automatically
  • How to set Oracle to start automatically at boot time under Linux
  • Oracle startup script under Linux and its startup
  • Oracle service start and stop scripts and boot auto-start in Linux
  • How to set Redis startup in Linux
  • Add a startup method to Linux (service/script)
  • Steps to start nodemanager at Linux boot
  • Redis password setting and auto-start under Linux
  • Detailed explanation of Linux boot process
  • How to start redis service automatically when Linux boots up

<<:  Vue implements paging function

>>:  A brief understanding of the difference between MySQL union all and union

Recommend

Core skills that web front-end development engineers need to master

The content involved in Web front-end development...

WeChat applet realizes multi-line text scrolling effect

This article example shares the specific code for...

Detailed explanation of how to use the Vue license plate search component

A simple license plate input component (vue) for ...

The background color or image inside the div container grows as it grows

Copy code The code is as follows: height:auto !im...

Use of Linux watch command

1. Command Introduction The watch command execute...

Vue+Router+Element to implement a simple navigation bar

This project shares the specific code of Vue+Rout...

How to smoothly upgrade and rollback Nginx version in 1 minute

Today, let's talk about a situation that is o...

Summary of various uses of JSON.stringify

Preface Anyone who has used json should know that...

Steps for Django to connect to local MySQL database (pycharm)

Step 1: Change DATABASES in setting.py # Configur...

Web Design Summary

<br />From the birth of my first personal pa...

Summary of common commands for Linux user and group management

This article summarizes the common commands for L...

Which scenarios in JavaScript cannot use arrow functions

Table of contents 1. Define object methods 2. Def...

How to implement a simple HTML video player

This article introduces the method of implementin...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...