Untitled
unknown
plain_text
a year ago
900 B
13
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