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-failureRestartSec=5s指示systemd在进程失败时尝试重启,每次尝试前等待5秒。

  3. 启用并启动服务

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

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

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

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

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

    bash 复制代码
    sudo apt install sendmail
    sudo sendmailconfig

    遵循安装提示完成配置。

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

    再次编辑myphp.service文件:

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

    添加ExecStartPostExecStopPost命令发送邮件:

    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

相关推荐
A小辣椒20 小时前
TShark:Wireshark CLI 功能
linux
A小辣椒1 天前
TShark:基础知识
linux
BingoGo1 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack1 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户3074596982072 天前
PHP 扩展——从入门到理解
php
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
鹏仔先生3 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php