Linux Add boot boot method of service and script

  • 2021-07-10 21:22:42
  • OfStack

Configuration files that need to be loaded when the system starts up

/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 rc.local Documents 
[root@localhost ~]# vi /etc/rc.local

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

# 3. Final modification rc.local Execute permission of file 
[root@localhost ~]# chmod +x /etc/rc.local
[root@localhost ~]# chmod 755 /etc/rc.local

2. Write your own shell script

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

3. Set through the chkconfig command


# 1. Will ( Script ) Startup file moved 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 /etc/rc.d/init.d

# 2. Be sure to add the following before the startup file 3 Line code, no side will prompt chkconfig Not supported. 
#!/bin/sh              Tells the system to use the shell, So shell Scripts are all like this 
#chkconfig: 35 20 80         Represents run level, startup priority, and shutdown priority, respectively. This line of code must 
#description: http server      Play by yourself! ! ! That this line of code must 
/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log

# 3. Increase the executable permissions of scripts 
chmod +x /etc/rc.d/init.d/test.sh

# 4. Add scripts to the boot auto-startup project. Add to chkconfig , boot self-startup. 
[root@localhost ~]# cd /etc/rc.d/init.d
[root@localhost ~]# chkconfig --add test.sh
[root@localhost ~]# chkconfig test.sh on

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

# 6. From chkconfig Delete in management test.sh
[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: Description service 
After: Describe the service category 

[Service] Setting of service running parameters 
Type=forking       Is the form of running in the background 
ExecStart         Run commands for the details of the service 
ExecReload        Restart command for service 
ExecStop         Stop command for service 
PrivateTmp=True      Indicates allocating independent temporary space to services 
 Note: Start, restart, and stop commands all require absolute paths 

[Install]         Settings related to service installation 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 startup and self-startup (executed in any directory). If an error is reported by executing the startup command, then: systemctl daemon-reload is executed


 Set boot self-startup 
[root@localhost ~]# systemctl enable nginx.service    
[root@localhost ~]# systemctl enable supervisord

 Stop startup and start automatically 
[root@localhost ~]# systemctl disable nginx.service
[root@localhost ~]# systemctl disable supervisord

 Validation 1 Is it boot-up under 
[root@localhost ~]# systemctl is-enabled nginx
[root@localhost ~]# systemctl is-enabled supervisord

4. Other orders


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

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

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

 View nginx Current state of service 
[root@localhost ~]# systemctl status nginx.service

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

5. Sample service file:


# supervisord.service Process management service file 
[Unit]
Description=Process Monitoring and Control Daemon  #  Content self-defined: 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


Related articles: