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

Introduction to commonly used MySQL commands in Linux environment

Enter the mysql command: mysql -u+(user name) -p+...

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

Detailed explanation of reduce fold unfold usage in JS

Table of contents fold (reduce) Using for...of Us...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

JS implements request dispatcher

Table of contents Abstraction and reuse Serial Se...

Use the more, less, and cat commands in Linux to view file contents

In Linux, the commands cat, more, and less can al...

Detailed installation tutorial of mysql 5.7.11 under Win7 system

Operating system: Win7 64-bit Ultimate Edition My...

Solve the problem of MySQL using not in to include null values

Notice! ! ! select * from user where uid not in (...

react-beautiful-dnd implements component drag and drop function

Table of contents 1. Installation 2.APi 3. react-...

Detailed use cases of vue3 teleport

Official Website https://cli.vuejs.org/en/guide/ ...

Methods and steps for deploying multiple war packages in Tomcat

1 Background JDK1.8-u181 and Tomcat8.5.53 were in...

Detailed explanation of slots in Vue

The reuse of code in vue provides us with mixnis....

MySql sets the specified user database view query permissions

1. Create a new user: 1. Execute SQL statement to...