没有任何人敢保证自己写的程序没有任何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

守护程序核心代码:

 #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 = ;
ok = false; //每秒钟定时询问心跳
timerHeart = new QTimer(this);
timerHeart->setInterval();
connect(timerHeart, SIGNAL(timeout()), this, SLOT(sendHearData())); //从6050端口开始,如果绑定失败则将端口加1,直到绑定成功
udp = new QUdpSocket(this);
int port = ;
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 = ;
ok = false;
} //如果超过规定次数没有收到心跳回复,则超时重启
if (count >= App::TimeoutCount) {
timerHeart->stop(); QSharedMemory mem(App::TargetAppName);
if (!mem.create()) {
killApp();
} QTimer::singleShot( , this, SLOT(killOther()));
QTimer::singleShot( , this, SLOT(startApp()));
QTimer::singleShot( , 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 = ;
return;
} QProcess *p = new QProcess;
p->start(QString("\"%1/%2.exe\"").arg(qApp->applicationDirPath()).arg(App::TargetAppName)); count = ;
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(, ));
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() == "OK") {
count = ;
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 = ;
if (ui->btnStart->text() == "暂停") {
timerHeart->stop();
ui->btnStart->setText("开始");
} else {
timerHeart->start();
ui->btnStart->setText("暂停");
}
} void frmMain::on_btnReset_clicked()
{
App::ReStartCount = ;
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, "提示", "重置配置文件成功!");
}

主程序使用代码:

 #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() - ).split(".").at();
} 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()));
}

Qt编写守护程序保证程序一直运行(开源)的更多相关文章

  1. Qt 编写多窗口程序

    该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处! 导语      程序要实现的功能是:程序开始出现一个对话框,按下按钮后便能进入主窗口,如果直接关闭 ...

  2. nohup保证程序后台运行

    前言 我们运行某些命令的时候,它会默认在前台执行.如果要进行其他操作,则需要先停掉此程序.然后就蛋疼了.   解决 碰到这种情况,我们可以使用"nohup"命令和"&am ...

  3. QT编写上位机程序一定要初始化变量以及谨慎操作指针

    背景: 在编写QT上位机界面时,界面在运行的时候经常出现卡死或者直接挂掉的怪现象. 正文: 上位机有个函数为check_receive():该函数的作用为定时调用循环检测USB是否有数据.若有,则将信 ...

  4. 转:Qt编写串口通信程序全程图文讲解

    转载:http://blog.csdn.net/yafeilinux/article/details/4717706  作者:yafeilinux (说明:我们的编程环境是windows xp下,在Q ...

  5. Qt编写串口通信程序全程图文讲解 .

    在Qt中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,我们这里也是使用的该类.我们可以去 http://sourceforge.net/projects/qex ...

  6. 【转】Qt编写串口通信程序全程图文讲解

    本文章原创于www.yafeilinux.com 转载请注明出处. (说明:我们的编程环境是windows xp下,在Qt Creator中进行,如果在Linux下或直接用源码编写,程序稍有不同,请自 ...

  7. Qt编写串口通信程序全程图文解说

    (说明:我们的编程环境是windows xp下,在Qt Creator中进行,假设在Linux下或直接用源代码编写,程序稍有不同,请自己修改.) 在Qt中并没有特定的串口控制类,如今大部分人使用的是第 ...

  8. CentOS 不间断会话(ssh关闭后如何保证程序继续运行)(nohup和screen)

    当使用ssh与远程主机的会话被关闭时,在远程主机上运行的命令也随之被中断. 就是ssh 打开以后,bash等都是他的子程序,一旦ssh关闭,系统将所有相关进程杀掉!! 导致一旦ssh关闭,执行中的任务 ...

  9. 【深入学习linux】在linux系统下怎么编写c语言程序并运行

    1. 首先安装下 gcc : centos yum -y gcc 2. 编写c程序保存hello.c: #include <stdio.h> #include <stdlib.h&g ...

随机推荐

  1. Scala:First Steps in Scala

    var and val 简单来说,val声明的变量可以重新修改其引用,val则不行,见下面的例子: def max(x: Int, y: Int): Int = { if(x > y) x el ...

  2. 你真的会用Gson吗?Gson使用指南(4)

    原文出处: 怪盗kidou 注:此系列基于Gson 2.4. 本次文章的主要内容: TypeAdapter JsonSerializer与JsonDeserializer TypeAdapterFac ...

  3. Shiro基础知识03----shiro授权(编程式授权),Permission详解,授权流程(zz)

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).  在权限认证中,最核心的是:主体/用户(Subject).权限(Permission).角色(Role).资源 ...

  4. 周期同步位置模式(CSP),轮廓位置模式(PPM),位置模式(PM)

    什么是运动控制? 运动控制就是通过机械传动装置对运动部件的位置.速度进行实时的控制管理,使运动部件按照预期的轨迹和规定的运动参数(如速度.加速度参数等)完成相应的动作. 运动控制系统的典型构成 1. ...

  5. grep与孪生兄弟egrep差异

    egrep是对grep的功能扩展,让其支持正则更加完美! #grep与egrep不同  egrep完全支持正则 ls |grep -i '[a-z]\{3\}'    === ls |egrep -i ...

  6. Spark 论文篇-Spark:工作组上的集群计算的框架(中英双语)

    论文内容: 待整理 参考文献: Spark: Cluster Computing with Working Sets. Matei Zaharia, Mosharaf Chowdhury, Micha ...

  7. bindpose定义

    根据常识,有: 但根据骨骼动画的定义,又有: 对比两式,可得bindpose的定义: 即骨骼bone的bindpose是:在绑定时刻,将顶点由mesh的局部空间变换到bone的局部空间的变换.

  8. Kubernetes1.2如何使用iptables

    转:http://blog.csdn.net/horsefoot/article/details/51249161 本次分析的kubernetes版本号:v1.2.1-beta.0. Kubernet ...

  9. 第三部分:Android 应用程序接口指南---第二节:UI---第四章 Action Bar

    第4章 Action Bar Action Bar是一个能用于确定应用程序和用户的位置,并提供给用户操作和导航模式的窗口功能.如果需要显著地展示当前用户的操作或导航,应该使用Action Bar,因为 ...

  10. vue-worker的介绍和使用

    vue-worker把复杂的web worker封装起来,提供一套非常简明的api接口,使用的时候可以说像不接触worker一样方便.那么具体怎么使用呢? 安装 npm i -S vue-worker ...