Untitled

 avatar
unknown
plain_text
5 months ago
900 B
11
Indexable
#include <unistd.h>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

void log_message(const string &message) {
    ofstream log_file("/tmp/memloggerd.log", ios::app);
    log_file << "[" << time(0) << "] " << message << endl;
}

int main() {
    pid_t pid = fork();
    if (pid < 0) {
        return EXIT_FAILURE; // Fork failed
    } else if (pid > 0) {
        return EXIT_SUCCESS; // Parent process terminates
    }

    if (setsid() < 0) {
        return EXIT_FAILURE; // Detach from terminal
    }

    int fd_count = sysconf(_SC_OPEN_MAX);
    for (int i = 0; i < fd_count; ++i) {
        close(i); // Close all file descriptors
    }

    log_message("Demon uruchomiony.");

    while (true) {
        system("free >> /tmp/meminfo.log");
        log_message("Zapisano stan pamięci i czas.");
        sleep(10);
    }

    return EXIT_SUCCESS;
}
Editor is loading...
Leave a Comment