学而实习之 不亦乐乎

Linux 添加服务或脚本开机启动

2023-11-04 06:19:28

首先,系统启动时需要加载的配置文件如下:

/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)

一、自己写一个shell脚本

在 Linux 系统中 /etc/profile.d/ 目录中存放的是一些应用程序所需的启动脚本,其中包括了颜色、语言、less、vim及which等命令的一些附加设置。
 这些脚本文件之所以能够被自动执行,是因为在/etc/profile 文件中使用一个 for 循环语句来调用这些脚本,而 /etc/profile 文件为系统的每个用户设置环境变量信息,此文件的修改会影响到所有用户。

虽然可以将要启动的命令写到 /etc/profile 文件中,但还是建议将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,这样系统启动后就会自动执行该目录下的所有shell 脚本。

二、使用 crontab  

crontab 是 Linux 下的计划任务,当时间达到我们设定的时间时,可以自动触发某些脚本的运行。crontab 还有一些特殊的时间方式,如 @reboot、@yearly 等。这里我们可以使用 @reboot 即可。

在 crontab 中 @reboot 表示这个任务就是在系统重启之后自动运行此命令。如下:

$ crontab -e
@reboot /home/ok/auto_run_script.sh

三、修改开机启动文件:/etc/rc.local(或者/etc/rc.d/rc.local)

注意:在早期版本中 /etc/rc.d/rc.local 文件会在 Linux 系统各项服务都启动完毕之后再被运行。所以你想要自己的脚本在开机后被运行的话,可以将自己脚本路径加到该文件里。这里只须修改rc.local文件,在 exit 0 前面加入你的命令。在 CentOS 7 中可以在此文件头看到:

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

所以,建议使用 systemd services,同时注意与以前的版本不同,由于在引导期间并行执行,此脚本不会在所有其他服务之后运行。

1.编辑rc.local文件

# vi /etc/rc.local

2.修改rc.local文件

/etc/init.d/mysqld start                     # mysql开机启动
/etc/init.d/nginx start                     # nginx开机启动
supervisord -c /etc/supervisor/supervisord.conf         # supervisord开机启动
/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null

3.最后修改rc.local文件的执行权限

# chmod +x /etc/rc.local
# chmod 755 /etc/rc.local

四、通过 chkconfig 命令设置

1.将(脚本)启动文件移动到 /etc/init.d/或者/etc/rc.d/init.d/目录下。(前者是后者的软连接)

# mv /www/wwwroot/test.sh /etc/rc.d/init.d 

2.启动文件前面务必添加如下三行代码,否侧会提示chkconfig不支持。

#!/bin/sh             告诉系统使用的shell,所以的shell脚本都是这样
#chkconfig: 35 20 80        分别代表运行级别,启动优先权,关闭优先权,此行代码必须
#description: http server     自己随便发挥!!!,此行代码必须
/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log

3.增加脚本的可执行权限

# chmod +x /etc/rc.d/init.d/test.sh

4.添加脚本到开机自动启动项目中。添加到chkconfig,开机自启动。

# cd /etc/rc.d/init.d
# chkconfig --add test.sh
# chkconfig test.sh on

5.关闭开机启动 

# chkconfig test.sh off

6.从chkconfig管理中删除test.sh

# chkconfig --del test.sh

7.查看chkconfig管理

# chkconfig --list test.sh

五、使用 systemd 系统

systemd 即为system daemon,是linux下的一种init软件,可以自定义服务文件,添加到系统服务,通过 Systemctl 命令进行管理。

1.写服务文件

如 nginx.service、redis.service、supervisord.service

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
 
[Service]服务运行参数的设置
Type=forking      是后台运行的形式
ExecStart        为服务的具体运行命令
ExecReload       为服务的重启命令
ExecStop        为服务的停止命令
PrivateTmp=True     表示给服务分配独立的临时空间

注意:启动、重启、停止命令全部要求使用绝对路径
[Install]        服务安装的相关设置,可设置为多用户
WantedBy=multi-user.target 

2.文件保存在目录下

以754的权限。目录路径:/usr/lib/systemd/system。如上面的supervisord.service文件放在这个目录下面。

# cat /usr/lib/systemd/system/nginx.service
# cat /usr/lib/systemd/system/supervisord.service

3.设置开机自启动(任意目录下执行)

如果执行启动命令报错,则执行:systemctl daemon-reload

设置开机自启动

# systemctl enable nginx.service    
# systemctl enable supervisord

停止开机自启动

# systemctl disable nginx.service
# systemctl disable supervisord

验证一下是否为开机启动

# systemctl is-enabled nginx
# systemctl is-enabled supervisord

4.其他命令

启动nginx服务
# systemctl start nginx.service
 
停止nginx服务
# systemctl start nginx.service
 
重启nginx服务
# systemctl restart nginx.service
 
查看nginx服务当前状态
# systemctl status nginx.service
 
查看所有已启动的服务
# systemctl list-units --type=service

5.服务文件示例

# supervisord.service进程管理服务文件

[Unit]
Description=Process Monitoring and Control Daemon  # 内容自己定义: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服务文件
[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服务文件
[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