make it loop and run the scripts

This commit is contained in:
Mats van Reenen 2020-12-25 19:20:40 +01:00
parent 571a23d614
commit 705fc119a4

View File

@ -1,9 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <dirent.h> #include <dirent.h>
#include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include "log.h" #include "log.h"
@ -66,7 +66,7 @@ time_t getNextTrigger(Script_t *script, time_t startTime){
} }
if(t.tm_sec != 0){ if(t.tm_sec != 0){
t.tm_sec = 0; t.tm_sec = 0;
if(t.tm_min = 59){ if(t.tm_min == 59){
t.tm_min = 0; t.tm_min = 0;
t.tm_hour++; t.tm_hour++;
}else{ }else{
@ -229,24 +229,36 @@ void getScripts(){
} }
void main(){ void main(){
while(1){
for(uint8_t i=MAX_NUMBER_OF_SCRIPTS; i > 0; i--){ for(uint8_t i=MAX_NUMBER_OF_SCRIPTS; i > 0; i--){
Scripts[i].next = 0; Scripts[i].next = 0;
} }
getScripts(); getScripts();
time_t nextScriptTrigger = time(NULL) + 30 * 24 * 60 * 60; time_t nextScriptTrigger = time(NULL) + 30 * 24 * 60 * 60;
Script_t *scriptToTrigger = NULL; char scriptToTrigger[] = "./scripts/";
char *cmd = NULL;
for(uint8_t i=MAX_NUMBER_OF_SCRIPTS; i > 0; i--){ for(uint8_t i=MAX_NUMBER_OF_SCRIPTS; i > 0; i--){
if(Scripts[i].next == 0) if(Scripts[i].next == 0)
continue; continue;
if(Scripts[i].next < nextScriptTrigger){ if(Scripts[i].next < nextScriptTrigger){
nextScriptTrigger = Scripts[i].next; nextScriptTrigger = Scripts[i].next;
scriptToTrigger = &Scripts[i]; cmd = strcat(&scriptToTrigger[0], &Scripts[i].filename[0]);
} }
} }
uint32_t timeToSleep = nextScriptTrigger - time(NULL); uint32_t timeToSleep = nextScriptTrigger - time(NULL);
logInfo("sleep for %d (%d:%2d:%2d) seconds to %s%s%s", #if DEBUG <= 2
if(cmd != NULL)
logInfo("sleep for %d (%d:%02d:%02d) seconds before running '%s'",
timeToSleep, timeToSleep/3600, (timeToSleep/60) % 60, timeToSleep % 60, timeToSleep, timeToSleep/3600, (timeToSleep/60) % 60, timeToSleep % 60,
(scriptToTrigger == NULL) ? "refrash scripts" : " run script '", scriptToTrigger + 10);
(scriptToTrigger == NULL) ? "" : scriptToTrigger->filename, else
(scriptToTrigger == NULL) ? "" : "'"); logInfo("sleep for %d (%d:%02d:%02d) seconds before refreshing scripts",
timeToSleep, timeToSleep/3600, (timeToSleep/60) % 60, timeToSleep % 60);
#endif
sleep(timeToSleep);
logInfo("running script '%s'", scriptToTrigger + 10);
cmd = strcat(cmd, " &");
system(cmd);
}
} }