Set Shell script boot self-starting methods and instances _linux shell_ script home

Set the method and instance of Shell script startup

Updated: Feb 23, 2024 at 10:40:14 Author: Seven-Dimensional Brain
This article mainly introduces the method and example of setting the Shell script to boot itself, the article gives you a very detailed introduction through the code example, which is helpful to your study or work, and the friends who need it can refer to it

1. Prepare a Shell script

#! /bin/sh #chkconfig: 2345 22 80 #description: Filebeat Service echo "start test ..." ; touch /data/aaa.txt sleep 3 echo "test over ..."

1.1 Attention

#! /bin/sh #chkconfig: 2345 22 80 #description: Filebeat Service

These three lines must be placed three lines before the script. Otherwise, it may appear that service xxx.sh does not support chkconfig or service xxx.sh does not support chkconfig

2. Copy the script to the init.d directory

If scripts are not managed in a unified manner, you can create scripts in the init.d directory.

Note that the script path is changed to its own path. If the script is directly created in the init.d directory, ignore the cp /data/test.sh /etc/init.d/test.sh command

3. Set script permissions

chmod +x /etc/init.d/test.sh

4. Add services

chkconfig --add test.sh

If the command does not work, switch to the /etc/init.d/ directory.

5. Check whether the configuration is successfully added

Replace # test.sh with your own script name chkconfig --list test.sh

2345 If enabled, the startup succeeds.

The following is supplementary science popularization, but do not look

6. Set the method and instance of Shell script startup

In Linux system, self-starting script is one of the important means to manage and configure the system. This chapter describes how to set up Shell scripts to run automatically when the system starts, and will focus on how to do this under different Linux systems.

6.1 Principles of Shell script Auto-startup

The self-launching of Shell scripts can be done by adding scripts to the directory to be executed at system startup or through the system service manager. The principles of the two methods are described below.

6.1.1 Adding a Directory to a Boot Directory

Linux systems automatically execute script files in specific directories at startup. By adding Shell scripts to these directories, you can start on your own.

  • /etc/rc.d/rc.local directory: In some traditional Linux distributions, scripts can be added to/etc/rc.d/rc.localIn the file. This file is executed last at system startup and can be used to customize startup scripts.
# Add the script path to the rc.local file echo "/path/to/your/script.sh" >> /etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local

6.1.2 Using System Service Manager

Modern Linux systems often use systemd or other service managers to control system services. By creating a systemd service unit, you can easily manage the self-starting of Shell scripts.

  • Create systemd service unit file:
# /etc/systemd/system/your-service.service
[Unit]
Description=Your Service Description
After=network.target

[Service]
ExecStart=/path/to/your/script.sh
Restart=always
User=your_username

[Install]
WantedBy=multi-user.target
  • Enable and start the service:
# enable service sudo systemctl enable your-service # enable service sudo systemctl start your-service

6.2 Specific implementation methods of different Linux systems

6.2.1 Ubuntu/Debian

In Ubuntu and Debian systems, Shell scripts can be self-started through the systemd service manager.

# Create the systemd service unit file sudo nano /etc/systemd/system/your-service.service

Add the service unit content mentioned above to the file, save it, and exit. Then enable and start the service.

sudo systemctl enable your-service
sudo systemctl start your-service

6.2.2 CentOS/RHEL

On CentOS and RHEL systems, you can use systemd or add scripts to the /etc/rc.d/rc.local file.

systemd

# Create the systemd service unit file sudo nano /etc/systemd/system/your-service.service

Add the service unit content, save and exit, and then enable and start the service.

sudo systemctl enable your-service
sudo systemctl start your-service

Added to rc.local

# Add the script path to the rc.local file echo "/path/to/your/script.sh" >> /etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local

6.2.3 Arch Linux

Arch Linux also uses systemd as a service manager. Similarly, self-boot can be achieved by creating a systemd service unit file.

# Create the systemd service unit file sudo nano /etc/systemd/system/your-service.service

Add the service unit content, save and exit, and then enable and start the service.

sudo systemctl enable your-service
sudo systemctl start your-service

The above is to set the Shell script boot self-start method and instance details, more about Shell script boot self-start information please pay attention to script home other related articles!

Related article

  • shell获取命令行参数示例分享

    shell get command line parameter example share

    This article mainly introduces the shell to get command line parameter example sharing, need friends can refer to the next
    2014-03-03
  • Shell编程 Bash引号的那点事

    Shell programming Bash quotes that thing

    I want to write this series of articles, because I see people always mention the same problems, make the same mistakes, I used to do so, I can't bear to see people often so tortuous over
    2015-01-01
  • 防止shell脚本重复执行的代码

    Code that prevents repeated execution of shell scripts

    The locking mechanism allows a particular shell script to run only one instance at a time. In particular, the script instance that obtains the lock can continue to execute the critical section code; If no instance of the lock is obtained, you can only wait
    2013-01-01
  • shell实现自动备份mysql、整站数据的两个脚本分享

    shell realize automatic backup mysql, the whole site data of two scripts to share

    This article mainly introduces the shell to realize automatic backup of mysql, the whole site data of two scripts to share, the use of cron to achieve periodic backup, and will automatically delete N days ago backup, as well as automatic FTP upload to other servers, need friends can refer to
    2014-06-06
  • Linux shell实现压缩多个文件代码实例

    The Linux shell implements compression of multiple file code instances

    This article mainly introduces the Linux shell to achieve compression of multiple file code examples, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, need friends can refer to it
    2020-08-08
  • shell判断变量是否含某个字符串的6种方法

    6 ways for the shell to determine whether a variable contains a string

    This article mainly introduces the shell to determine whether the variable contains a string of 6 methods, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to learn it
    2023-05-05
  • linux下怎么解压.tar.gz .tar.bz2命令

    How to decompress the.tar.gz. tar.bz2 command in linux

    This article mainly introduces the linux decompression.tar.gz. tar.bz2 command related information, very good, with reference value, need friends can refer to the next
    2016-12-12
  • ssh,scp自动登陆的实现方法

    ssh, the implementation method of scp automatic login

    This article mainly introduces ssh,scp automatic landing implementation method, need friends can refer to the next
    2013-02-02
  • Shell常用操作符总结

    Summary of common Shell operators

    This article mainly introduces the Shell commonly used operators summary, this article explains the arithmetic operators, relational operators, test operators and other content, need friends can refer to the next
    2015-05-05
  • Shell中关于处理方法返回值问题详解

    A detailed explanation of the problem of handling method return values in Shell

    Recently, I have come into contact with some text processing on Linux, and the amount of data is still quite large. It is inevitable that the semester will be spent on scripting languages such as shell and awk. The following article mainly introduces the relevant information about the Shell about the return value problem of the processing method, and the friends who need it can refer to it. Let's take a look at it below.
    2017-12-12

Latest comments