目录
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 常用命令
| 命令 | 作用 |
|---|---|
|
| 检查配置文件语法是否正确。 |
|
| 启动 Monit 守护进程。 |
|
| 重载配置文件,使更改生效。 |
|
| 查看所有监控项的详细状态。 |
|
| 查看特定监控项(如 nginx)的状态。 |
|
| 启动所有监控的服务。 |
|
| 停止所有监控的服务。 |
|
| 重启特定的监控服务(如 nginx) |
6.5 Web 界面
Monit 提供了一个内置的 Web 界面(默认端口 2812),让你可以通过浏览器直观地查看所有监控服务的状态。
- 确保配置中已启用 HTTPD 模块(如之前配置示例所示)。
- 在浏览器中访问
http://your-server-ip:2812。 - 使用配置文件中设置的用户名和密码(如
admin:monit)登录。
你就可以在网页上看到所有服务的状态、系统资源使用情况,并能进行简单的启动、停止、重启等操作。
1739

被折叠的 条评论
为什么被折叠?



