Add a startup method to Linux (service/script)

Add a startup method to Linux (service/script)

Configuration file that needs to be loaded when the system starts

/etc/profile, /root/.bash_profile
/etc/bashrc, /root/.bashrc
/etc/profile.d/*.sh, /etc/profile.d/lang.sh
/etc/sysconfig/i18n, /etc/rc.local (/etc/rc.d/rc.local)

1. Modify the boot startup file: /etc/rc.local (or /etc/rc.d/rc.local)

# 1. Edit the rc.local file [root@localhost ~]# vi /etc/rc.local

# 2. Modify the rc.local file and add the following command before exit 0. Save and exit.
/etc/init.d/mysqld start #mysql startup/etc/init.d/nginx start #nginx startup supervisord -c /etc/supervisor/supervisord.conf # supervisord startup/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null

# 3. Finally, modify the execution permission of the rc.local file [root@localhost ~]# chmod +x /etc/rc.local
[root@localhost ~]# chmod 755 /etc/rc.local

2. Write a shell script yourself

Put the written script (.sh file) in the directory /etc/profile.d/. All shell scripts in this directory will be automatically executed after the system starts.

3. Set through chkconfig command

# 1. Move the (script) startup file to the /etc/init.d/ or /etc/rc.d/init.d/ directory. (The former is a soft link to the latter)
mv /www/wwwroot/test.sh /etc/rc.d/init.d

# 2. Be sure to add the following three lines of code before the startup file, otherwise it will prompt that chkconfig is not supported.
#!/bin/sh tells the system to use the shell, so all shell scripts are like this #chkconfig: 35 20 80 represent the run level, startup priority, and shutdown priority respectively. This line of code must #description: http server You can use it as you like! ! ! This line of code must be /bin/echo $(/bin/date +%F_%T) >> /tmp/test.log

# 3. Add executable permissions to the script chmod +x /etc/rc.d/init.d/test.sh

# 4. Add the script to the automatic startup project. Add to chkconfig and start automatically at boot.
[root@localhost ~]# cd /etc/rc.d/init.d
[root@localhost ~]# chkconfig --add test.sh
[root@localhost ~]# chkconfig test.sh on

# 5. Turn off boot [root@localhost ~]# chkconfig test.sh off

# 6. Delete test.sh from chkconfig management
[root@localhost ~]# chkconfig --del test.sh

# 7. View chkconfig management [root@localhost ~]# chkconfig --list test.sh

4. Customize service files, add them to system services, and manage them through Systemctl

1. Write service files: such as nginx.service, redis.service, supervisord.service

[Unit]: Description of the service Description: Describe the service After: Describe the service category [Service] Settings for service running parameters Type=forking is the form of background running ExecStart is the specific running command of the service ExecReload is the restart command of the service ExecStop is the stop command of the service PrivateTmp=True means to allocate independent temporary space to the service Note: All start, restart, and stop commands require the use of absolute paths [Install] Related settings for service installation, which can be set to multi-user WantedBy=multi-user.target 

2. The file is saved in the directory: with 754 permissions. Directory path: /usr/lib/systemd/system. For example, the supervisord.service file above is placed under this directory.

[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service

3. Set the system to start automatically at boot (execute in any directory). If an error occurs when executing the startup command, execute: systemctl daemon-reload

Set up automatic startup [root@localhost ~]# systemctl enable nginx.service    
[root@localhost ~]# systemctl enable supervisord

Stop booting automatically [root@localhost ~]# systemctl disable nginx.service
[root@localhost ~]# systemctl disable supervisord

Verify whether it is started at boot [root@localhost ~]# systemctl is-enabled nginx
[root@localhost ~]# systemctl is-enabled supervisord

4. Other commands

Start nginx service [root@localhost ~]# systemctl start nginx.service

Stop nginx service [root@localhost ~]# systemctl start nginx.service

Restart nginx service [root@localhost ~]# systemctl restart nginx.service

View the current status of the nginx service [root@localhost ~]# systemctl status nginx.service

View all started services [root@localhost ~]# systemctl list-units --type=service

5. Service file example:

# supervisord.service process management service file [Unit]
Description=Process Monitoring and Control Daemon # Define the content yourself: Description=Supervisor daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop= /usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload
Restart=on-failure
RestartSec=42s
KillMode=process 

[Install]
WantedBy=multi-user.target

# nginx.service service file [Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

# redis.service service file [Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www

[Install]
WantedBy=multi-user.target

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

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
  • 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
  • Example of how to set up a Linux system to automatically run a script at startup

<<:  vue $set implements assignment of values ​​to array collection objects

>>:  Four modes of Oracle opening and closing

Recommend

A complete guide to Linux environment variable configuration

Linux environment variable configuration When cus...

Remote development with VSCode and SSH

0. Why do we need remote development? When develo...

Implementation of form submission in html

Form submission code 1. Source code analysis <...

A brief discussion on tags in HTML

0. What is a tag? XML/HTML CodeCopy content to cl...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

How to change password in MySQL 5.7.18

How to change the password in MySQL 5.7.18: 1. Fi...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

How to represent various MOUSE shapes

<a href="http://" style="cursor...

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the I...

Basic introductory tutorial on MySQL partition tables

Preface In a recent project, we need to save a la...

Binary Type Operations in MySQL

This article mainly introduces the binary type op...