-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
83 lines (69 loc) · 2.24 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For sleep(), fork(), access(), readlink()
#include <sys/wait.h> // For waitpid()
#include <string.h>
#include <libgen.h> // For dirname()
#define MAX_PATH_LENGTH 4096
#define ERROR_EXIT(msg) { perror(msg); exit(EXIT_FAILURE); }
void execute_command(const char *command) {
pid_t pid = fork();
if (pid < 0) {
ERROR_EXIT("fork() error");
}
if (pid == 0) {
// Child process
execlp("python3", "python3", command, (char *)NULL);
// If execlp returns, an error occurred
ERROR_EXIT("execlp() error");
} else {
// Parent process
int status;
if (waitpid(pid, &status, 0) == -1) {
ERROR_EXIT("waitpid() error");
}
if (WIFEXITED(status)) {
printf("Child process exited with status %d\n", WEXITSTATUS(status));
} else {
printf("Child process terminated abnormally\n");
}
}
}
void timer(int secs, const char *script_dir) {
while (1) {
sleep(secs * 60);
// Build the command string
char command[MAX_PATH_LENGTH];
snprintf(command, sizeof(command), "%s/set_next.py", script_dir);
// Check if the file exists
if (access(command, F_OK) != 0) {
fprintf(stderr, "File %s does not exist.\n", command);
continue;
}
// Execute the command
execute_command(command);
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <time>\n", argv[0]);
return EXIT_FAILURE;
}
int time = atoi(argv[1]);
if (time <= 0) {
fprintf(stderr, "Invalid time argument. It must be a positive integer.\n");
return EXIT_FAILURE;
}
// Get the directory of the currently running executable
char exe_path[MAX_PATH_LENGTH];
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
if (len == -1) {
ERROR_EXIT("readlink() error");
}
exe_path[len] = '\0'; // Null-terminate the path
// Extract the directory from the executable path
char *dir = dirname(exe_path);
// Start the timer with the directory containing set_next.py
timer(time, dir);
return EXIT_SUCCESS;
}