The method of adding boot script to linux
How to add Linux boot script:
Configuration files that need to be loaded at system startup
/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)
First, modify the boot file: /etc/rc.local (or/etc/rc.d/rc.local)
# 1. Edit rc.local file [root@localhost ~]# vi /etc/rc.local # 2. Modify rc.local file by adding the following command before exit 0. Save and exit./ etc/init.d/mysqld start The requested URL/etc/init.d/nginx/was not found on this server. # nginx boot supervisor-c /etc/supervisor/supervisor.conf # supervisord boot/bin/bash /server/scripts/test.sh>/dev/null 2>/dev/null # 3. Finally modify the rc.local file execution permissions [root@localhost ~]# chmod +x /etc/rc.local[root@localhost ~]# chmod 755 /etc/rc.local
Write your own shell script.
Put the script (.sh file) in the directory/etc/profile.d/, and all shell scripts in that directory will be executed automatically after the system boots.
3. Set through chkconfig command
# 1. Move the (script) boot file to/etc/init.d/or/etc/rc.d/init.d/directory. (The former is the soft connection of the latter) mv /www/wwwroot/test.sh.d/init.d # 2. Be sure to add the following three lines of code in front of the startup file. No side will prompt chkconfig not supported.#!/ bin/sh Tell the system which shell to use, all shell scripts are like this #chkconfig: 35 20 80 This line code must be #description: http server. Do it yourself!! This line of code must be/bin/echo $(/bin/date +%F_%T) >>/tmp/test. log # 3. Add executable permissions for scripts chmod +x /etc/rc.d/init.d/test.sh # 4. Add scripts to the boot autostart project. Add to chkconfig, boot from 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. Remove test.sh[root@ localhost ~]# chkconfig --del test.sh # 7. View chkconfig admin [root@localhost ~]# chkconfig --list test.sh
IV. Custom service files, added to system services, managed through Systemctl
1. Write service files such as nginx.service, redis.service, supervisor.service
[Unit]: Description of Service Description: Description of Service After: Description of Service Category [Service] Setting of Service Operating Parameters Type=forking is a background run form ExecStart Execute Reload for specific run commands of the service Restart command for service ExecuStop Stop command for service PrivateTmp=True Indicates the allocation of independent temporary space for services Note: Start, restart, stop commands all require absolute paths [Install] Settings related to service installation, which can be set to multi-user WantedBy=multi-user.target
2. File saved in directory: with 754 permissions. Directory path: /usr/lib/systemd/system. 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 boot to boot (execute under any directory). If an error is reported when executing the startup command, execute: systemctl daemon-reload
[root@localhost ~]# systemctl enable nginx.service [root@localhost ~]# systemctl enable supervisor Stop boot auto-boot [root@ localhost ~]# systemctl disable nginx.service[root@localhost ~]# systemctl disable supervisor Verify boot [root@localhost ~]# systemctl is-enabled nginx[root@ localhost ~]# systemctl is-enabled supervisor
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. Examples of service files:
#supervisor.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= forkingExecuStart =/usr/bin/supervisor Executor-c /etc/supervisor/supervisor. confExecuStop = /usr/bin/supervisorctl shutdownExecuReload =/usr/bin/supervisorctl reloadRestart=on-failureRestartSec=42sKillMode=process [Install] W @ edBy =multi-user.target# nginx.service service [Unit]Description=nginx - high performance web serverAfter=network.target remote-fs.targetnss-lookup.target [Service]Type= forkingExecuStart =/usr/local/nginx/sbin/nginx Inx-c /usr/local/nginx/conf/nginx. confExecuReload =/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop [Install]WantedBy=multi-user.target# redis.service service [Unit]Description=RedisAfter=network. remote-fs.targetnss-lookup.target [Service]Type=forkingExecStart=/usr/local/bin/redis-server /etc/redis. confExecuStop =kill -INT `cat /tmp/redis.pid`User=wwwGroup=www [Install]WantedBy=multi-user.target
So how do I add a Linux boot script? For more details, please pay attention to other related articles!