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

Front-end vue+express file upload and download example

Create a new server.js yarn init -y yarn add expr...

Vue sample code for implementing two-column horizontal timeline

Table of contents 1. Implement the component time...

The normal method of MySQL deadlock check processing

Normally, when a deadlock occurs, the connection ...

MySQL series: Basic concepts of MySQL relational database

Table of contents 1. Basic Concepts 2. Developmen...

Comprehensive website assessment solution

<br />Sometimes you may be asked questions l...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

Nginx proxy axios request and precautions

Preface I recently wrote a small demo. Because I ...

How to install theano and keras on ubuntu system

Note: The system is Ubuntu 14.04LTS, a 32-bit ope...

How to build a deep learning environment running Python in Docker container

Check virtualization in Task Manager, if it is en...

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue watch Notice Name: You s...

Tutorial on installing lamp-php7.0 in Centos7.4 environment

This article describes how to install lamp-php7.0...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Discussion on image path issues in css (same package/different package)

In CSS files, sometimes you need to use background...