Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add a boot script to Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to add startup scripts to Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Configuration files that need 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 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 by adding the following command before exit 0. Save and exit. / etc/init.d/mysqld start # mysql boot / etc/init.d/nginx start # nginx boot supervisord-c / etc/supervisor/supervisord.conf # supervisord boot / bin/bash / server/scripts/test.sh > / dev/null 2 > / dev/null# 3. Finally, modify the execution permission of rc.local file [root@localhost ~] # chmod + x / etc/rc.local [root@localhost ~] # chmod 755 / etc/rc.local

Write a shell script by yourself

Put the written script (.sh file) in the directory / etc/profile.d/, and all shell scripts in that directory will be executed automatically when the system starts.

Third, set up through the chkconfig command

# 1. Move the startup file to the / etc/init.d/ or / etc/rc.d/init.d/ directory. (the former is the soft connection of 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 the side will prompt chkconfig not to support it. #! / bin/sh tells the shell used by the system that all shell scripts are like this. # chkconfig: 35 20 80 represents the run level, start priority, and turn off priority, respectively. This line of code must be # description: http server play freely! This line of code must be / bin/echo $(/ bin/date +% favored% T) > > / tmp/test.log# 3. Add executable permissions to the script chmod + x / etc/rc.d/init.d/test.sh# 4. Add a script to the boot auto startup project. Add to chkconfig, boot automatically. [root@localhost] # cd / etc/rc.d/init.d [root@localhost ~] # chkconfig-- add test.sh [root@localhost ~] # chkconfig test.sh on# 5. Turn it off and start [root@localhost ~] # chkconfig test.sh off# 6. Remove test.sh [root@localhost ~] # chkconfig from chkconfig management-- del test.sh# 7. View chkconfig Management [root@localhost ~] # chkconfig-- list test.sh

Custom service files, added to system services, and managed through Systemctl

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

[Unit]: description of service Description: description of service After: description of service category [Service] setting of service operation parameters Type=forking is the form of background operation specific operation command ExecStart for service specific operation command ExecReload for service restart command ExecStop for service stop command PrivateTmp=True indicates allocation of independent temporary space to service attention: start, restart, Stop commands all require the relevant settings of the absolute path [Install] service installation Can be set to multi-user WantedBy=multi-user.target

two。 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 up boot self-boot (executed in any directory). If an error is reported by executing the startup command, execute: systemctl daemon-reload

Set [root@localhost ~] # systemctl enable nginx.service [root@localhost ~] # systemctl enable supervisord to stop boot [root@localhost ~] # systemctl disable nginx.service [root@localhost ~] # systemctl disable supervisord to verify whether to 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 nginx service current status [root@localhost ~] # systemctl status nginx.service view all started services [root@localhost ~] # systemctl list-units-- type=service

5. Example of service file:

# supervisord.service process management service file [Unit] Description=Process Monitoring and Control Daemon # content definition: Description=Supervisor daemonAfter=rc-local.service nss-user- lookup.target [Service] Type=forkingExecStart=/usr/bin/supervisord-c / etc/supervisor/supervisord.confExecStop= / usr/bin/supervisorctl shutdown ExecReload=/usr/bin/supervisorctl reloadRestart=on-failureRestartSec=42sKillMode=process [Install] WantedBy=multi-user.target# nginx.service service file [Unit] Description=nginx-high performance web serverAfter=network.target remote-fs.target nss-lookup.target [Service] Type=forkingExecStart=/usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx-s reloadExecStop=/usr/local/nginx/sbin/nginx-s stop[ install] WantedBy=multi-user.target# redis.service service file [Unit] Description=RedisAfter=network.target remote-fs.target nss-lookup.target [Service] Type=forkingExecStart=/usr/local/bin/redis-server / etc/redis.confExecStop=kill-INT `cat / tmp/ redis.pid`User = wwwGroup= www [install] WantedBy=multi-user.target What is a Linux system Linux is a free-to-use and free-spread UNIX-like operating system Is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

This is the end of the article on "how to add a boot script for Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report