CentOS7中服务开机自启动的几种配置方法
  TEZNKK3IfmPf 2023年11月12日 49 0
[Unit]
Description=nacos server          # 描述信息
Wants=network-online.target       # Requires/Wants依赖关系, 多个服务之间用空格分隔
After=network.target              # After/Before 先后顺序 , 多个服务之间用空格分隔
 
[Service]
Type=                             # simple(默认值)/forking/oneshot/notify/dbus/idle
User=                             # 指定服务运行的用户
Group=                            # 指定服务运行的用户组
WorkingDirectory=                 # 工作目录,需要提前创建
PIDFile=                          # 指定守护进程PID文件
ExecStartPre=                     # 指定服务启动前的操作
ExecStart=                        # 指定启动脚本或命令
ExecStartPost=                    # 指定服务启动后的操作
ExecReload=                       # 指定服务reload脚本
ExecStop=                         # 指定服务停止脚本
ExecStopPost=                     # 指定服务停止后操作
TimeoutStartSec=                  # 服务启动超时
TimeoutStopSec=                   # 服务停止超时
TimeoutSec=                       # 相当于同时配置 TimeoutStartSec 和 TimeoutStopSec
WatchdogSec=                      # 状态检测间隔
EnvironmentFile=                  # 指定环境变量文件
Restart=on-failure                # 自动启动条件,no(默认值)/on-success/on-failure/always
RestartSec=10s                    # 重启服务间隔
StartLimitInterval=10s            # 重启时间频率,设置为0表示无限重启
StartLimitBurst=5                 # 重启次数,默认10秒内5次超出则不再重启
SuccessExitStatus=                # 自定义成功退出状态

[Install]
WantedBy=multi-user.target        # 指定服务所在系统启动级别在run level 3

restart策略配置:

退出原因

always

on-failure

on-success

on-abnormal

on-abort

on-watchdog

正常退出

 

 

 

 

退出码不为0

 

 

 

 

进程被强制杀死

 

 

systemd操作超时

 

 

 

看门狗超时

 

 

例子:vim /lib/systemd/system/lampp.service

[Unit]
Description=lampp
After=network.target
   
[Service]
Type=forking
ExecStart=/opt/lampp/lampp start
ExecReload=/opt/lampp/lampp restart
ExecStop=/opt/lampp/lampp  stop
   
[Install]
WantedBy=multi-user.target

编辑好了,就保存退出。用下面的命令来修改文件权限:

chmod 754 /lib/systemd/system/lampp.service

设置开机启动:

systemctl enable lampp.service

查看所有已启动的服务:

systemctl list-unit-files

二、rc.local配置开机自启动

1、rc-local.service需设置开机启动

查看状态:systemctl is-enabled rc-local.service

设置开机启动:systemctl enable rc-local.service

 2、/etc/rc.d/rc.local需要赋执行权限

chmod +x /etc/rc.d/rc.local

chmod +x /etc/rc.local

3、在/etc/rc.d/rc.local里面配置需要执行的脚本

/usr/local/nginx/sbin/nginx

su - cloudapp -c "/home/ap/cloudapp/scripts/server_start.sh"

三、crontab配置开机自启动

1、设置crond服务自身为开机自启动

查看状态:systemctl is-enabled crond.service

设置开机启动:systemctl enable crond.service

2、crontab -e添加自启动

@reboot /home/ap/cloudapp/scripts/server_start.sh

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年04月26日   39   0   0 linux服务器centos
  TEZNKK3IfmPf   2023年11月15日   32   0   0 centos中文
  TEZNKK3IfmPf   2023年11月15日   26   0   0 centos
  TEZNKK3IfmPf   2024年04月26日   34   0   0 linuxnginxcentos
TEZNKK3IfmPf