沒有任何人敢保證自己寫的程序沒有任何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 }
