/*******************************************************************
* Copyright 2009 Tycho Andersen.
*
* File: tcrond.c
*
* Purpose: simple cron with second resolution instead of minute.
* Usage: see file README
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Changelog:
* 0.2a: Fixed a small regression with sequential process handling.
* 0.2: Fixed serious issue with parallel mode which would eventually
* fill up the process table.
* 0.1: Initial release.
******************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_LINE_LENGTH 1024
#define MAX_ARG_COUNT 15
#define INITIAL_LIST_SIZE 11
typedef struct
{
uint32_t modulus, offset;
time_t next;
char *command;
char *args[MAX_ARG_COUNT];
} job;
/**************************************************************
* Function: next_job
* INPUT: job list, the size of the list, and the current time
* OUTPUT: the function stores the list of jobs to be run at the time
* returned in the list to_execute. This list is terminated by -1 if
* not all jobs are to be executed.
* to_execute should be at least count elements long.
**************************************************************/
time_t next_job(job* list, uint32_t count, int32_t* to_execute)
{
uint32_t min = UINT32_MAX, i, index = 0;
for(i=0; i now)
{
printf("sleeping for %lu...\n", (uint32_t) target-now);
sleep(target - now);
}
// execute next jobs
for(i=0; i 0)
num_exited++;
}
child_process_count -= num_exited;
}
}
free(to_execute);
for(i=0; i