查看完整版本: [-- Qt编写守护程序保证程序一直运行(开源) --]

QTCN开发网 -> Qt 作品展 -> Qt编写守护程序保证程序一直运行(开源) [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

liudianwu 2019-03-02 14:48

Qt编写守护程序保证程序一直运行(开源)

没有任何人敢保证自己写的程序没有任何BUG,尤其是在商业项目中,程序量越大,复杂度越高,出错的概率越大,尤其是现场环境千差万别,和当初本地电脑测试环境很可能不一样,有很多特殊情况没有考虑到,如果需要保证程序7*24小时运行,则需要想一些办法能够让程序死了能够活过来,在嵌入式linux上,大部分会采用看门狗的形式来处理,程序打开看门狗驱动后,定时喂狗,一旦超过规定的时间,则硬件软复位等。这种方式相对来说比较可靠,如果需要在普通PC机上运行怎办呢?本篇文章提供一个软件实现守护进程的办法,原理就是udp通信,单独写个守护进程程序,专门负责检测主程序是否存在,不存在则启动。主程序只需要启动live类监听端口,收到hello就回复ok就行。
为了使得兼容任意程序,特意提炼出来共性,增加了多种设置。
1:可设置检测的程序名称。
2:可设置udp通信端口。
3:可设置超时次数。
4:自动记录已重启次数。
5:自动记录最后一次重启时间。
6:是否需要重新刷新桌面。
7:可重置当前重启次数和最后重启时间。
8:自动隐藏的托盘运行或者后台运行。
9:提供界面设置程序名称已经开启和暂停服务。
代码下载:[attachment=19955]
[attachment=19952][attachment=19954][attachment=19953]

守护进程核心代码:
  1. #pragma execution_character_set("utf-8")
    #include "frmmain.h"
    #include "ui_frmmain.h"
    #include "qtimer.h"
    #include "qudpsocket.h"
    #include "qsharedmemory.h"
    #include "qprocess.h"
    #include "qdatetime.h"
    #include "qapplication.h"
    #include "qdesktopservices.h"
    #include "qmessagebox.h"
    #if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
    #include "qstandardpaths.h"
    #endif

    #include "app.h"

    frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
    {
        ui->setupUi(this);
        this->initForm();
    }

    frmMain::~frmMain()
    {
        delete ui;
    }

    void frmMain::changeEvent(QEvent *event)
    {
        //隐藏当前界面,最小化到托盘
        if(event->type() == QEvent::WindowStateChange) {
            if(windowState() & Qt::WindowMinimized) {
                hide();
            }
        }

        QWidget::changeEvent(event);
    }

    void frmMain::initForm()
    {
        count = 0;
        ok = false;

        //每秒钟定时询问心跳
        timerHeart = new QTimer(this);
        timerHeart->setInterval(2000);
        connect(timerHeart, SIGNAL(timeout()), this, SLOT(sendHearData()));

        //从6050端口开始,如果绑定失败则将端口加1,直到绑定成功
        udp = new QUdpSocket(this);
        int port = 6050;
        while(!udp->bind(port)) {
            port++;
        }

        connect(udp, SIGNAL(readyRead()), this, SLOT(readData()));

        if (App::TargetAppName.isEmpty()) {
            ui->btnStart->setText("启动");
            ui->btnStart->setEnabled(false);
            timerHeart->stop();
        } else {
            ui->btnStart->setText("暂停");
            ui->btnStart->setEnabled(true);
            timerHeart->start();
        }

        ui->txtAppName->setText(App::TargetAppName);
        ui->txtAppName->setFocus();
    }

    void frmMain::sendHearData()
    {
        udp->writeDatagram("hello", QHostAddress::LocalHost, App::TargetAppPort);

        //判断当前是否没有回复
        if (!ok) {
            count++;
        } else {
            count = 0;
            ok = false;
        }

        //如果超过规定次数没有收到心跳回复,则超时重启
        if (count >= App::TimeoutCount) {
            timerHeart->stop();

            QSharedMemory mem(App::TargetAppName);
            if (!mem.create(1)) {
                killApp();
            }

            QTimer::singleShot(1000 , this, SLOT(killOther()));
            QTimer::singleShot(3000 , this, SLOT(startApp()));
            QTimer::singleShot(4000 , this, SLOT(startExplorer()));
        }
    }

    void frmMain::killApp()
    {
        QProcess *p = new QProcess;
        p->start(QString("taskkill /im %1.exe /f").arg(App::TargetAppName));
    }

    void frmMain::killOther()
    {
        QProcess *p = new QProcess;
        p->start(QString("taskkill /im %1.exe /f").arg("WerFault"));

        //重建缓存,彻底清除托盘图标
        if (App::ReStartExplorer) {
            QProcess *p1 = new QProcess;
            p1->start("taskkill /f /im explorer.exe");
        }
    }

    void frmMain::startApp()
    {
        if (ui->btnStart->text() == "开始" || ui->btnStart->text() == "启动") {
            count = 0;
            return;
        }

        QProcess *p = new QProcess;
        p->start(QString("\"%1/%2.exe\"").arg(qApp->applicationDirPath()).arg(App::TargetAppName));

        count = 0;
        ok = true;
        timerHeart->start();

        App::ReStartCount++;
        App::ReStartLastTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
        App::writeConfig();

        ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount));
        ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime));
    }

    void frmMain::startExplorer()
    {
        //取得操作系统目录路径,指定操作系统目录下的explorer程序,采用绝对路径,否则在64位操作系统下无效
    #if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
        QString str = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
    #else
        QString str = QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation);
    #endif

        if (App::ReStartExplorer) {
            str = QString("%1\\Windows\\explorer.exe").arg(str.mid(0, 2));
            QProcess *p = new QProcess(this);
            p->start(str);
        }
    }

    void frmMain::readData()
    {
        QByteArray tempData;
        do {
            tempData.resize(udp->pendingDatagramSize());
            udp->readDatagram(tempData.data(), tempData.size());
            QString data = QLatin1String(tempData);
            if (data.right(2) == "OK") {
                count = 0;
                ok = true;
            }
        } while (udp->hasPendingDatagrams());
    }

    void frmMain::on_btnOk_clicked()
    {
        App::TargetAppName = ui->txtAppName->text();
        if (App::TargetAppName == "") {
            QMessageBox::critical(this, "提示", "应用程序名称不能为空!");
            ui->txtAppName->setFocus();
            return;
        }

        App::writeConfig();
        ui->btnStart->setEnabled(true);
    }

    void frmMain::on_btnStart_clicked()
    {
        count = 0;
        if (ui->btnStart->text() == "暂停") {
            timerHeart->stop();
            ui->btnStart->setText("开始");
        } else {
            timerHeart->start();
            ui->btnStart->setText("暂停");
        }
    }

    void frmMain::on_btnReset_clicked()
    {
        App::ReStartCount = 0;
        App::ReStartLastTime = "2019-01-01 12:00:00";
        App::writeConfig();

        ui->txtAppName->setText(App::TargetAppName);
        ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount));
        ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime));
        QMessageBox::information(this, "提示", "重置配置文件成功!");
    }
主程序使用核心代码:
  1. #include "applive.h"
    #include "qmutex.h"
    #include "qudpsocket.h"
    #include "qstringlist.h"
    #include "qapplication.h"
    #include "qdatetime.h"
    #include "qdebug.h"

    #define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))

    QScopedPointer<AppLive> AppLive::self;
    AppLive *AppLive::Instance()
    {
        if (self.isNull()) {
            QMutex mutex;
            QMutexLocker locker(&mutex);
            if (self.isNull()) {
                self.reset(new AppLive);
            }
        }

        return self.data();
    }

    AppLive::AppLive(QObject *parent) : QObject(parent)
    {
        udpServer  = new QUdpSocket(this);

        QString name = qApp->applicationFilePath();
        QStringList list = name.split("/");
        appName = list.at(list.count() - 1).split(".").at(0);
    }

    void AppLive::readData()
    {
        QByteArray tempData;

        do {
            tempData.resize(udpServer->pendingDatagramSize());
            QHostAddress sender;
            quint16 senderPort;
            udpServer->readDatagram(tempData.data(), tempData.size(), &sender, &senderPort);
            QString data = QLatin1String(tempData);

            if (data == "hello") {
                udpServer->writeDatagram(QString("%1OK").arg(appName).toLatin1(), sender, senderPort);
            }
        } while (udpServer->hasPendingDatagrams());
    }

    bool AppLive::start(int port)
    {
        bool ok = udpServer->bind(port);
        if (ok) {
            connect(udpServer, SIGNAL(readyRead()), this, SLOT(readData()));
            qDebug() << TIMEMS << "Start AppLive Ok";
        }

        return ok;
    }

    void AppLive::stop()
    {
        udpServer->abort();
        disconnect(udpServer, SIGNAL(readyRead()), this, SLOT(readData()));
    }




九重水 2019-03-02 16:19
没电,电脑挂了,连自己都守护不了,看你怎么守护别人

liudianwu 2019-03-02 17:35
九重水:没电,电脑挂了,连自己都守护不了,看你怎么守护别人[表情] [表情] [表情] [表情]  (2019-03-02 16:19) 

来你过来,我保证不打屎你!

leytou 2019-03-02 17:56
感谢大佬分享

xzp21st 2019-03-02 18:06
九重水:没电,电脑挂了,连自己都守护不了,看你怎么守护别人[表情] [表情] [表情] [表情]  (2019-03-02 16:19) 

你这种情况得考虑上ups了

wcp520 2019-03-03 15:16
感谢大佬分享

九重水 2019-03-04 09:51
xzp21st:你这种情况得考虑上ups了 (2019-03-02 18:06) 

正解。

liuchangyin 2019-03-04 10:45
这个是保证软件运行的最后一道保险,我也曾这样做过

liudianwu 2019-03-04 11:57
liuchangyin:这个是保证软件运行的最后一道保险,我也曾这样做过[表情]  (2019-03-04 10:45) 

对的,作用就是:软件崩溃自重启!

liuyuanan 2019-04-24 20:10
我们的解决方案如下:写一个进程管理程序,这个程序使用QProcess类负责启动和关闭进程,当QProcess类监测到进程挂掉后,就重启该进程,这种方案不需要原有的进程做任何修改

cericking 2019-05-05 01:18
liudianwu:对的,作用就是:软件崩溃自重启! (2019-03-04 11:57) 

其实9楼的方法也很好

QProcess 对于一个进程打开和关闭是两种不同的状态,
可以每隔一段时间 如100ms,检测进程的状态 如果关闭了,则重启程序

更简洁一些 不占用网络资源

chamsdoncon 2019-05-06 10:27
9楼的方法更适合windows系统吧,大佬这个适合跨平台

greensky10 2019-05-14 17:59
我们也是用9楼的方法,在Linux底下用着没问题啊!

0011411 2019-11-19 13:31
        

liaohanghua 2020-11-25 15:43
不错 值得学习 谢谢分享

jobfind 2021-10-18 16:30


查看完整版本: [-- Qt编写守护程序保证程序一直运行(开源) --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled