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

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

守护程序核心代码:

  1 #pragma execution_character_set("utf-8")
  2 #include "frmmain.h"
  3 #include "ui_frmmain.h"
  4 #include "qtimer.h"
  5 #include "qudpsocket.h"
  6 #include "qsharedmemory.h"
  7 #include "qprocess.h"
  8 #include "qdatetime.h"
  9 #include "qapplication.h"
 10 #include "qdesktopservices.h"
 11 #include "qmessagebox.h"
 12 #if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
 13 #include "qstandardpaths.h"
 14 #endif
 15 
 16 #include "app.h"
 17 
 18 frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
 19 {
 20     ui->setupUi(this);
 21     this->initForm();
 22 }
 23 
 24 frmMain::~frmMain()
 25 {
 26     delete ui;
 27 }
 28 
 29 void frmMain::changeEvent(QEvent *event)
 30 {
 31     //隐藏当前界面,最小化到托盘
 32     if(event->type() == QEvent::WindowStateChange) {
 33         if(windowState() & Qt::WindowMinimized) {
 34             hide();
 35         }
 36     }
 37 
 38     QWidget::changeEvent(event);
 39 }
 40 
 41 void frmMain::initForm()
 42 {
 43     count = 0;
 44     ok = false;
 45 
 46     //每秒钟定时询问心跳
 47     timerHeart = new QTimer(this);
 48     timerHeart->setInterval(2000);
 49     connect(timerHeart, SIGNAL(timeout()), this, SLOT(sendHearData()));
 50 
 51     //从6050端口开始,如果绑定失败则将端口加1,直到绑定成功
 52     udp = new QUdpSocket(this);
 53     int port = 6050;
 54     while(!udp->bind(port)) {
 55         port++;
 56     }
 57 
 58     connect(udp, SIGNAL(readyRead()), this, SLOT(readData()));
 59 
 60     if (App::TargetAppName.isEmpty()) {
 61         ui->btnStart->setText("启动");
 62         ui->btnStart->setEnabled(false);
 63         timerHeart->stop();
 64     } else {
 65         ui->btnStart->setText("暂停");
 66         ui->btnStart->setEnabled(true);
 67         timerHeart->start();
 68     }
 69 
 70     ui->txtAppName->setText(App::TargetAppName);
 71     ui->txtAppName->setFocus();
 72 }
 73 
 74 void frmMain::sendHearData()
 75 {
 76     udp->writeDatagram("hello", QHostAddress::LocalHost, App::TargetAppPort);
 77 
 78     //判断当前是否没有回复
 79     if (!ok) {
 80         count++;
 81     } else {
 82         count = 0;
 83         ok = false;
 84     }
 85 
 86     //如果超过规定次数没有收到心跳回复,则超时重启
 87     if (count >= App::TimeoutCount) {
 88         timerHeart->stop();
 89 
 90         QSharedMemory mem(App::TargetAppName);
 91         if (!mem.create(1)) {
 92             killApp();
 93         }
 94 
 95         QTimer::singleShot(1000 , this, SLOT(killOther()));
 96         QTimer::singleShot(3000 , this, SLOT(startApp()));
 97         QTimer::singleShot(4000 , this, SLOT(startExplorer()));
 98     }
 99 }
100 
101 void frmMain::killApp()
102 {
103     QProcess *p = new QProcess;
104     p->start(QString("taskkill /im %1.exe /f").arg(App::TargetAppName));
105 }
106 
107 void frmMain::killOther()
108 {
109     QProcess *p = new QProcess;
110     p->start(QString("taskkill /im %1.exe /f").arg("WerFault"));
111 
112     //重建缓存,彻底清除托盘图标
113     if (App::ReStartExplorer) {
114         QProcess *p1 = new QProcess;
115         p1->start("taskkill /f /im explorer.exe");
116     }
117 }
118 
119 void frmMain::startApp()
120 {
121     if (ui->btnStart->text() == "开始" || ui->btnStart->text() == "启动") {
122         count = 0;
123         return;
124     }
125 
126     QProcess *p = new QProcess;
127     p->start(QString(""%1/%2.exe"").arg(qApp->applicationDirPath()).arg(App::TargetAppName));
128 
129     count = 0;
130     ok = true;
131     timerHeart->start();
132 
133     App::ReStartCount++;
134     App::ReStartLastTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
135     App::writeConfig();
136 
137     ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount));
138     ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime));
139 }
140 
141 void frmMain::startExplorer()
142 {
143     //取得操作系统目录路径,指定操作系统目录下的explorer程序,采用绝对路径,否则在64位操作系统下无效
144 #if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
145     QString str = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
146 #else
147     QString str = QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation);
148 #endif
149 
150     if (App::ReStartExplorer) {
151         str = QString("%1\Windows\explorer.exe").arg(str.mid(0, 2));
152         QProcess *p = new QProcess(this);
153         p->start(str);
154     }
155 }
156 
157 void frmMain::readData()
158 {
159     QByteArray tempData;
160     do {
161         tempData.resize(udp->pendingDatagramSize());
162         udp->readDatagram(tempData.data(), tempData.size());
163         QString data = QLatin1String(tempData);
164         if (data.right(2) == "OK") {
165             count = 0;
166             ok = true;
167         }
168     } while (udp->hasPendingDatagrams());
169 }
170 
171 void frmMain::on_btnOk_clicked()
172 {
173     App::TargetAppName = ui->txtAppName->text();
174     if (App::TargetAppName == "") {
175         QMessageBox::critical(this, "提示", "应用程序名称不能为空!");
176         ui->txtAppName->setFocus();
177         return;
178     }
179 
180     App::writeConfig();
181     ui->btnStart->setEnabled(true);
182 }
183 
184 void frmMain::on_btnStart_clicked()
185 {
186     count = 0;
187     if (ui->btnStart->text() == "暂停") {
188         timerHeart->stop();
189         ui->btnStart->setText("开始");
190     } else {
191         timerHeart->start();
192         ui->btnStart->setText("暂停");
193     }
194 }
195 
196 void frmMain::on_btnReset_clicked()
197 {
198     App::ReStartCount = 0;
199     App::ReStartLastTime = "2019-01-01 12:00:00";
200     App::writeConfig();
201 
202     ui->txtAppName->setText(App::TargetAppName);
203     ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount));
204     ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime));
205     QMessageBox::information(this, "提示", "重置配置文件成功!");
206 }

主程序使用代码:

 1 #include "applive.h"
 2 #include "qmutex.h"
 3 #include "qudpsocket.h"
 4 #include "qstringlist.h"
 5 #include "qapplication.h"
 6 #include "qdatetime.h"
 7 #include "qdebug.h"
 8 
 9 #define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
10 
11 QScopedPointer<AppLive> AppLive::self;
12 AppLive *AppLive::Instance()
13 {
14     if (self.isNull()) {
15         QMutex mutex;
16         QMutexLocker locker(&mutex);
17         if (self.isNull()) {
18             self.reset(new AppLive);
19         }
20     }
21 
22     return self.data();
23 }
24 
25 AppLive::AppLive(QObject *parent) : QObject(parent)
26 {
27     udpServer  = new QUdpSocket(this);
28 
29     QString name = qApp->applicationFilePath();
30     QStringList list = name.split("/");
31     appName = list.at(list.count() - 1).split(".").at(0);
32 }
33 
34 void AppLive::readData()
35 {
36     QByteArray tempData;
37 
38     do {
39         tempData.resize(udpServer->pendingDatagramSize());
40         QHostAddress sender;
41         quint16 senderPort;
42         udpServer->readDatagram(tempData.data(), tempData.size(), &sender, &senderPort);
43         QString data = QLatin1String(tempData);
44 
45         if (data == "hello") {
46             udpServer->writeDatagram(QString("%1OK").arg(appName).toLatin1(), sender, senderPort);
47         }
48     } while (udpServer->hasPendingDatagrams());
49 }
50 
51 bool AppLive::start(int port)
52 {
53     bool ok = udpServer->bind(port);
54     if (ok) {
55         connect(udpServer, SIGNAL(readyRead()), this, SLOT(readData()));
56         qDebug() << TIMEMS << "Start AppLive Ok";
57     }
58 
59     return ok;
60 }
61 
62 void AppLive::stop()
63 {
64     udpServer->abort();
65     disconnect(udpServer, SIGNAL(readyRead()), this, SLOT(readData()));
66 }
原文地址:https://www.cnblogs.com/feiyangqingyun/p/10461166.html