Linux服务器设置服务开机自启的6种方式

目录

1. 使用systemd服务(最推荐)

2. 使用 chkconfig(SysVinit 系统)

3. 使用 /etc/rc.local

4. 使用 crontab 的 @reboot

5. 使用Supervisor守护进程

5.1 安装软件包

5.2 配置文件

5.3 启动和管理守护进程

6. Monit-系统监控与进程管理工具

6.1 软件安装

6.2 主配置文件

​​6.3 服务监控配置​

示例1:

示例2:

示例3:

6.4 常用命令

6.5 Web 界面


1. 使用systemd服务(最推荐)

通常在目录 /usr/lib/systemd/system/或 /etc/systemd/system/创建

vim  /etc/systemd/system/my-service.service

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
User=myuser
WorkingDirectory=/path/to/working/directory
ExecStart=/usr/bin/python3 /path/to/your/script.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

然后启用服务:

sudo systemctl daemon-reload
sudo systemctl enable my-service
sudo systemctl start my-service

2. 使用 chkconfig(SysVinit 系统)

# 1. 创建服务脚本(/etc/init.d/myscript)
#!/bin/bash
# 启动命令
start() {
  /path/to/script.sh
}

# 2. 赋予可执行权限
chmod +x /etc/init.d/myscript

# 3. 添加到启动项
chkconfig --add myscript
chkconfig myscript on

3. 使用 /etc/rc.local

# 1. 创建脚本(例如:/root/startup.sh)
#!/bin/bash
echo "开机自启脚本执行" >> /var/log/startup.log
# 添加你的命令

# 2. 赋予可执行权限
chmod +x /root/startup.sh

# 3. 编辑rc.local文件
sudo vim   /etc/rc.local

# 在exit 0之前添加:
/root/startup.sh

# 4. 确保rc.local有执行权限
sudo chmod +x /etc/rc.local

4. 使用 crontab 的 @reboot

# 1. 编辑crontab
crontab -e

# 2. 添加以下行(在文件末尾)
@reboot /path/to/script.sh

# 3. 保存并退出

5. 使用Supervisor守护进程

5.1 安装软件包

#CentOS/RHEL:
yum install -y epel-release
yum install -y supervisor

#Ubuntu/Debian:
sudo apt-get update
sudo apt-get install supervisor

5.2 配置文件

Supervisor的配置文件结构:

  • 主配置文件:/etc/supervisord.conf
  • 配置文件目录:/etc/supervisor/conf.d/ 或 /etc/supervisord.d/
  • 日志目录:/var/log/supervisor/ 或自定义路径
[include]
files = /etc/supervisord/*.conf
在 /etc/supervisor/conf.d/ 目录下创建配置文件
[program:my_django]
command = /home/lmzf/.virtualenvs/fasterenv/bin/python manage.py runserver 0.0.0.0:8800
directory = /home/lmzf/FasterRunner
stopasgroup = true
stopsignal = QUIT
user = lmzf
autostart = true
autorestart = true
stderr_logfile = /home/lmzf/err.log
stdout_logfile = /home/lmzf/out.log

5.3 启动和管理守护进程

#启动Supervisor服务
systemctl start supervisord    

#重新加载配置
supervisorctl update  或  supervisorctl reload

#启动特定进程
supervisorctl start my_django

#设置开机自启
systemctl enable supervisord

6. Monit-系统监控与进程管理工具

6.1 软件安装

sudo yum install epel-release
sudo yum install monit
sudo systemctl start monit
sudo systemctl enable monit
sudo systemctl status monit

6.2 主配置文件

Monit 的配置通常涉及一个主配置文件(如 /etc/monitrc)和可选的额外配置文件目录(如 /etc/monit.d/

主配置文件用于设置全局参数,一个基本的配置示例如下:

# 设置检查间隔为30秒,启动后延迟10秒开始第一次检查
set daemon 30
with start delay 10

# 设置日志文件位置
set logfile /var/log/monit.log

# 设置邮件服务器,用于发送警报
set mailserver smtp.example.com port 587
    username "your_email@example.com" password "your_password"
    using tlsv1

# 设置邮件格式
set mail-format {
    from: monit@example.com
    subject: [Monit Alert] $SERVICE $EVENT at $DATE
    message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}

# 接收警报的邮箱
set alert your_email@example.com

# 启用Web界面,设置端口、允许访问的IP及认证信息
set httpd port 2812
    allow admin:monit  # 用户名:密码
    allow localhost    # 允许本地访问

# 包含/etc/monit.d/目录下的所有.conf配置文件
include /etc/monit.d/*

重要​​:为确保安全,Monit 主配置文件的权限不应超过 0700

sudo chmod 600 /etc/monitrc

​6.3 服务监控配置​

你可以在 /etc/monit.d/目录下为每个服务创建单独的配置文件。

示例1:

以下是一个简单的Monit配置示例,用于监控Apache服务:

# /etc/monit.d/apache
check process apache with pidfile /var/run/apache2.pid
  start program = "/etc/init.d/apache2 start"
  stop program = "/etc/init.d/apache2 stop"
  if failed port 80 protocol http then restart
  if 5 restarts within 5 cycles then timeout

示例2:

# /etc/monit.d/nginx
check process nginx with pidfile /var/run/nginx.pid
    start program = "/usr/bin/systemctl start nginx"
    stop program = "/usr/bin/systemctl stop nginx"
    if failed host 127.0.0.1 port 80 protocol http then restart
    if 5 restarts within 5 cycles then timeout
  • if failed host 127.0.0.1 port 80...: 如果端口 80 的 HTTP 检查失败,则重启服务。

  • if 5 restarts within 5 cycles then timeout: 这是​​安全机制​​。如果 5 个检查周期内重启了 5 次,则停止重启,防止因配置错误等问题导致无限重启

示例3:

# /etc/monit.d/myapp
check process myapp with pidfile /var/run/myapp.pid
    start program = "/usr/bin/java -jar /path/to/myapp.jar"
    stop program = "/bin/kill -TERM $(cat /var/run/myapp.pid)"
    if cpu > 80% for 2 cycles then alert
    if totalmem > 500 MB for 5 cycles then restart

6.4 常用命令

命令

作用

sudo monit -t

​检查配置文件语法​​是否正确。

sudo monit

启动 Monit 守护进程。

sudo monit reload

​重载​​配置文件,使更改生效。

sudo monit status

查看​​所有监控项​​的详细状态。

sudo monit status nginx

查看​​特定监控项​​(如 nginx)的状态。

sudo monit start all

启动所有监控的服务。

sudo monit stop all

停止所有监控的服务。

sudo monit restart nginx

重启特定的监控服务(如 nginx)

6.5 Web 界面

Monit 提供了一个内置的 Web 界面(默认端口 2812),让你可以通过浏览器直观地查看所有监控服务的状态。

  • 确保配置中已启用 HTTPD 模块(如之前配置示例所示)。
  • 在浏览器中访问 http://your-server-ip:2812
  • 使用配置文件中设置的用户名和密码(如 admin:monit)登录。

你就可以在网页上看到所有服务的状态、系统资源使用情况,并能进行简单的启动、停止、重启等操作。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值