Domanda

I have very simple code that should run on background and at 1 am shut down the computer:

#include <ctime>
#include <cstdlib>
#include <unistd.h>

int main() {
    time_t t;struct tm * now;
    daemon(0,0);
    while(1){
        t = time(0);
        now = localtime( & t );
        if(now->tm_hour==1){
           system("shutdown -P");
           break;
        }
        sleep(10);
    }
    return 0;
} 

The code works without sleep(10) but uses whole free memory so I need sleep function there to stop loop and recheck time each ten seconds, but with sleep function program stops immediately after I run it.

È stato utile?

Soluzione

If you are writing C code, don't use C++ headers (ctime, cstdlib). Replace those #includes with #include <stdlib.h> and #include <time.h>. If the behavior of this code is really as you describe (which I would find surprising), then this is probably the source of the error.

Altri suggerimenti

Of course it immediately exits. Thats the whole point of using daemon. Check with ps and you will see that your proram is still running as a seperate process now.

Check the man page for a desription how daemon works.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top