linux命令行启动一个php进程, 如何监控 ,崩溃报警通知?

在Linux环境中,监控PHP进程并在其崩溃时发送通知可以通过多种方式实现,包括使用shell脚本结合监控工具和通知服务。下面我将提供一个简单的方案,使用systemd服务来管理PHP进程,并通过邮件发送崩溃通知。

第1步:创建一个PHP脚本

假设您已经有一个PHP脚本,比如叫做script.php,位于/home/user/目录下。

第2步:创建一个systemd服务单元文件

  1. 创建服务文件 :

    打开一个文本编辑器来编写systemd服务单元文件,如nano:

    bash 复制代码
    sudo nano /etc/systemd/system/myphp.service
  2. 编辑服务文件 :

    在打开的编辑器中,输入以下内容:

    ini 复制代码
    [Unit]
    Description=My PHP Service
    After=network.target
    
    [Service]
    Type=simple
    User=user
    ExecStart=/usr/bin/php /home/user/script.php
    Restart=on-failure
    RestartSec=5s
    
    [Install]
    WantedBy=multi-user.target

    这里ExecStart指定了PHP的执行命令和脚本路径。Restart=on-failure和RestartSec=5s指示systemd在进程失败时尝试重启,每次尝试前等待5秒。

  3. 启用并启动服务 :

    使服务在启动时自动启动,并立即启动服务:

    bash 复制代码
    sudo systemctl enable myphp.service
    sudo systemctl start myphp.service

第3步:设置邮件发送崩溃通知

我们可以使用systemd的邮件通知功能,但首先需要在系统上配置邮件服务(如sendmail或postfix)。

  1. 安装sendmail(仅作示例,您也可以使用其他邮件服务器):

    bash 复制代码
    sudo apt install sendmail
    sudo sendmailconfig

    遵循安装提示完成配置。

  2. 修改服务文件以添加邮件通知:

    再次编辑myphp.service文件:

    bash 复制代码
    sudo nano /etc/systemd/system/myphp.service

    添加ExecStartPost和ExecStopPost命令发送邮件:

    ini 复制代码
    [Service]
    Type=simple
    User=user
    ExecStart=/usr/bin/php /home/user/script.php
    Restart=on-failure
    RestartSec=5s
    ExecStartPost=/usr/bin/mail -s "PHP Service Started" your@email.com <<< "PHP Service has started."
    ExecStopPost=/usr/bin/mail -s "PHP Service Stopped" your@email.com <<< "PHP Service has stopped."
  3. 重新加载并重启服务:

    bash 复制代码
    sudo systemctl daemon-reload
    sudo systemctl restart myphp.service

这样设置后,每次PHP服务启动或停止时,都会向指定的电子邮件地址发送通知。

其他方案

除了使用systemd和邮件,还有许多其他方法可以实现监控和通知,例如使用第三方监控工具(如Nagios, Zabbix, Prometheus等)和专业的错误追踪服务(如Sentry, Rollbar等)。这些工具和服务提供了更高级的监控功能和通知选项,可以根据需要进行选择。

标题发送邮件通知改成php脚本 qq邮箱 发送邮件

如果你想通过PHP脚本使用QQ邮箱发送邮件作为通知手段,你可以使用PHP的邮件发送库PHPMailer。首先,需要正确设置QQ邮箱以使用SMTP服务,然后配置PHPMailer来发送邮件。

步骤 1: 设置QQ邮箱

  1. 登录你的QQ邮箱。
  2. 进入设置 -> 账户。
  3. 找到"POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务"部分,开启"SMTP服务"并记录下生成的授权码,这个授权码将用作SMTP密码。

步骤 2: 安装PHPMailer

使用Composer安装PHPMailer。如果你没有安装Composer,请先安装Composer。

bash 复制代码
composer require phpmailer/phpmailer

步骤 3: 创建PHP脚本来发送邮件

创建一个PHP脚本,例如sendmail.php,并使用以下代码来配置PHPMailer发送邮件:

php 复制代码
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host       = 'smtp.qq.com';                    // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                             // Enable SMTP authentication
    $mail->Username   = 'your@qq.com';                    // SMTP username
    $mail->Password   = 'your-smtp-password';             // SMTP password (use the authorization code)
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 465;                              // TCP port to connect to

    // Recipients
    $mail->setFrom('your@qq.com', 'Mailer');
    $mail->addAddress('recipient@example.com', 'Joe User');     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'PHP Service Notification';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

步骤 4: 修改systemd服务文件

修改你的systemd服务文件,使其在服务启动或停止时运行这个PHP脚本。

ini 复制代码
[Service]
Type=simple
User=user
ExecStart=/usr/bin/php /home/user/script.php
Restart=on-failure
RestartSec=5s
ExecStartPost=/usr/bin/php /path/to/sendmail.php
ExecStopPost=/usr/bin/php /path/to/sendmail.php

记得替换/path/to/sendmail.php为你的PHP脚本实际路径。

步骤 5: 重新加载并重启服务

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl restart myphp.service

这样,每次服务启动或停止时,都会通过QQ邮箱发送邮件通知。确保SMTP设置和授权码正确,否则邮件发送可能会失败。

by chatgpt3.5

相关推荐
lzx_00212 小时前
Linux 进程(三) 从进程状态到僵尸进程、孤儿进程
linux·服务器
FlightYe13 小时前
音视频修炼之容器与协议(四):RTSP详解
android·linux·ffmpeg·音视频·dash
刘马想放假14 小时前
CANviz 使用指南|Linux 安装、DBC 解码、实时曲线与 J1939 实战
linux·python·websocket
kloc15 小时前
聊天室项目入口网关子服务的实现
linux·c++
GeW15 小时前
深圳APEC峰会倒计时:数据库与Linux运维的"隐形考场"
linux
梦到啥说啥16 小时前
Linux网络编程:UDP网络协议
linux·网络协议·udp
无足鸟ICT18 小时前
【RHCA+】ipcrm命令(删除IPC资源)
linux
星恒随风19 小时前
MySQL 从零入门(四):表的增删改查与基础查询详解
linux·服务器·数据库·笔记·mysql
陈希12321 小时前
Linux 网络诊断工具实战:ping、curl、tcpdump、mtr、dig 用法详解
linux·网络·tcpdump
进击的荆棘1 天前
Linux系统——线程同步与互斥之线程池和安全
linux·运维·服务器·安全·单例模式·线程