[Qt-interest] QProcess's bug?

邵度 shao.tu at gmail.com
Thu Jun 25 14:07:33 CEST 2009


OK. Let me shows a complete code and it's logs.
Please anybody help me to resolve this problem.
Complete codes as follows:

===== File: Shell.h =====
#ifndef Shell_H
#define Shell_H
#include <QtCore/QDate>
#include <QtCore/QFile>
#include <QtCore/QTimer>
#include <QtCore/QObject>
#include <QtCore/QProcess>


class Shell : public QObject
{
Q_OBJECT

public:
    Shell();

public slots:
    void run();
    void stop();

private slots:
    void purgeFileStarted();
    void purgeFileFinished(int exitCode, QProcess::ExitStatus exitStatus);

private:
    QFile _log;
    QTimer _timer;
    QProcess _shell;

    void purgeFiles(const QDate& currentDate);
};


#endif


===== File: Shell.cpp =====
#include <QtCore/QString>
#include <QtCore/QDateTime>
#include <QtCore/QIODevice>
#include <QtCore/QTextStream>
#include "Shell.h"

Shell::Shell()
{
    _log.setFileName("log.txt");
    _log.open(QIODevice::ReadWrite | QIODevice::Text);
    connect(&_shell, SIGNAL(started()), this, SLOT(purgeFileStarted()));
    connect(&_shell, SIGNAL(finished(int, QProcess::ExitStatus)),
            this, SLOT(purgeFileFinished(int, QProcess::ExitStatus)));
    connect(&_timer, SIGNAL(timeout()), this, SLOT(run()));
    _timer.start(2000);
}

void Shell::run()
{
    purgeFiles(QDateTime::currentDateTime().date());
}

void Shell::purgeFiles(const QDate& currentDate)
{
    static QDate last = QDate();

    QTextStream ts(&_log);
    if (_shell.state() != QProcess::NotRunning)
    {
        ts << "The process to purge file not finished yet "
           << "since last starting. Ignore this run. Status: "
           << _shell.state() << endl;
        return;
    }

    if (last != currentDate)
    {
        last = currentDate;
        QStringList args;
        args << getenv("TEST_DIR") << "-depth" << "-mtime" << "1" <<
"-print"
             << "-exec" << "rm" << "-rf" << "{}" << ";";
        _shell.start("find", args);
    }
}

void Shell::stop()
{
    if (_shell.state() != QProcess::NotRunning)
        _shell.waitForFinished(-1);
}

void Shell::purgeFileStarted()
{
    QTextStream ts(&_log);
    ts << "Purging files under '" << getenv("TEST_DIR") << "'"
       << " directory and older than 1 days..." << endl;
}

void Shell::purgeFileFinished(int code, QProcess::ExitStatus status)
{
    QTextStream ts(&_log);
    if (code != 0)
    {
        ts << "Error purging files. Standard output: "
           << _shell.readAllStandardOutput()
           << ", standard error: "
           << _shell.readAllStandardError()
           << endl;
    }
    else
    {
        QString purgedFiles = _shell.readAllStandardOutput().trimmed();
        if (purgedFiles.isEmpty())
            ts << "Done to purge files and no log files need purge." <<
endl;
        else
            ts << "Files were purged as follows:\n" << purgedFiles << endl;
    }
}


===== File: main.cpp =====
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <QtCore/QCoreApplication>
#include "Shell.h"


int main(int argc, char* argv[])
{
    QCoreApplication app(argc, argv);

    char c;
    bool daemon = false;
    while ((c = getopt(argc, argv, "d")) != -1)
    {
        switch (c)
        {
            case 'd':
                daemon = true;
                break;
            default:
                break;
        }
    }

    if (daemon)
    {
        if (fork() != 0)
            exit(0);
        setsid();
        if (fork() != 0)
            exit(0);

        close(0);
        close(1);
        close(2);
        open("/dev/null", O_RDONLY);
        open("/dev/null", O_WRONLY);
        open("/dev/null", O_WRONLY);
        umask(0);
    }

    Shell shell;
    return app.exec();
}


===== Logs without -d option given =====
Purging files under '/home/alex/TEST_DIR' directory and older than 1 days...
Files were purged as follows:
/home/alex/TEST_DIR/z.sh


===== Logs with -d option given =====
Purging files under '/home/alex/TEST_DIR' directory and older than 1 days...
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2
The process to purge file not finished yet since last starting. Ignore this
run. Status: 2


2009/6/25 Andreas Pakulat <apaku at gmx.de>

> On 25.06.09 15:58:51, 邵度 wrote:
> > My program looks like the following:
> >
> > int main(int argc, char* argv[])
> > {
> >   QCoreApplication app(argc, argv);
> >
> >   if (runInDaemon)
> >     becomeDaemonFromNowOn();  // By means of traditional unix fork()
> >
> >   ClassUsesQProcess obj;
> >
> >   return app.exec();
> > }
>
> This is not a proper example. This doesn't tell us:
>
> - how becomeDaemonFromNowOn  is implemented
> - how ClassUsesQProcess is created
> - how the QProcess is being used.
>
> You need to assemble a small runnable example for somebody to reproduce the
> issue to help you.
>
> Andreas
>
> --
> Give your very best today.  Heaven knows it's little enough.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090625/3a423b7c/attachment.html 


More information about the Qt-interest-old mailing list