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