闲碎记事本 闲碎记事本
首页
  • JAVA
  • Cloudflare
  • 学完再改一遍UI
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

YAN

我要偷偷记录...
首页
  • JAVA
  • Cloudflare
  • 学完再改一遍UI
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • java

  • linux

    • Shell基础
    • OpenEuler系统JAVA部署问题
    • sshpass 使用
    • 固定机器ip
    • 一些可修改的系统设置
    • 服务自启
      • 注册服务
        • 相关命令
        • 启动服务
        • 停止
        • 查看服务状态
        • 重启服务
        • 启用服务自启
        • 禁用服务自启
        • 查看系统服务列表
        • 重载系统服务
      • chkconfig
      • initd目录下放置脚本
        • 相关命令
        • 设置执行权限
        • 移动到目录下
      • rc.local文件编写命令
        • 相关命令
        • 设置执行权限
        • 编辑rc.local
    • 一些查询命令
    • Swap空间
    • 虚拟机磁盘扩展
  • docker

  • redis

  • nginx

  • mysql

  • 其他

  • 环境搭建

  • 知识库
  • linux
YAN
2023-07-21
目录

服务自启

服务器每天关机,每次都得手动重启服务,烦死了!要不整个自启吧。

研究了下大概有4种方式:

  • 注册服务
  • chkconfig
  • /etc/init.d 目录下放置脚本
  • /etc/rc.d/rc.local 文件编写命令

# 注册服务

在Centos 中 systemctl 是设置系统服务的命令, 即 service, 它融合之前service和chkconfig的功能于一体。 可以使用它永久性或只在当前会话中启用/禁用服务。

编写服务 test.service

[Unit]
Description=Test Server Name
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart= 需要执行的命令

[Install]
WantedBy=multi-user.target

复制到 /usr/lib/systemd/system/ 目录下

# 相关命令

# 启动服务

systemctl start  test

# 停止

systemctl stop  test

# 查看服务状态

systemctl status  test

# 重启服务

systemctl reload  test

# 启用服务自启

systemctl enable  test

# 禁用服务自启

systemctl disable  test

# 查看系统服务列表

systemctl list-unit-files –type=service

# 重载系统服务

systemctl daemon-reload

# chkconfig

tetschkconfig (opens new window)

# 注册系统服务
chkconfig --add service.sh
# 删除系统服务
chkconfig --del service.sh
# 设置开机启动
chkconfig service.sh on
# 查看系统服务列表
chkconfig --list
#启动服务
service service.sh start
#停止服务
service service.sh stop
#重启服务
service service.sh restart
#服务状态
service service.sh status

# initd目录下放置脚本

# 相关命令

# 设置执行权限

chmod +x  service.sh

# 移动到目录下

mv service.sh /etc/init.d/

# rc.local文件编写命令

rc.local是CentOS以前版本的方式,在CentOS7中仍然以兼容的形式存在,虽仍可用,但不推荐(推荐使用systemd service

# 相关命令

# 设置执行权限

chmod +x  rc.local

# 编辑rc.local

注意

在文件内写入自己要执行的命令,命令尽量使用后台运行命令,不要使用挂起命令,可能会影响系统启动。

vi  /etc/rc.d/rc.local
上次更新: 2025/05/22, 07:52:48
一些可修改的系统设置
一些查询命令

← 一些可修改的系统设置 一些查询命令→

最近更新
01
Caddy操作指南
04-25
02
虚拟机磁盘扩展
04-22
03
Swap空间
04-22
更多文章>
Theme by Vdoing | Copyright © 2022-2025 YAN | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式