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

Mysql master-slave synchronization configuration scheme under Centos7 system

Preface Recently, when working on a high-availabi...

MySQL data operation-use of DML statements

illustrate DML (Data Manipulation Language) refer...

IIS configuration of win server 2019 server and simple publishing of website

1. First remotely connect to the server 2. Open S...

Understanding of haslaylout and bfc parsing

1. haslayout and bfc are IE-specific and standard ...

Detailed explanation of the use of vue-resource interceptors

Preface Interceptor In some modern front-end fram...

Solve the installation problem of mysql8.0.19 winx64 version

MySQL is an open source, small relational databas...

How to place large images in a small space on a web page

Original source: www.bamagazine.com There are nar...

Summary of various methods for JS data type detection

Table of contents background What are the methods...

JavaScript Factory Pattern Explained

Table of contents Simple Factory Factory Method S...

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...

Vue implements two routing permission control methods

Table of contents Method 1: Routing meta informat...

VMware Workstation 14 Pro installation and activation graphic tutorial

This article shares the installation and activati...

Summary of basic usage of CSS3 @media

//grammar: @media mediatype and | not | only (med...

CentOS 6 uses Docker to deploy redis master-slave database operation example

This article describes how to use docker to deplo...