1

I am learning C at the moment but I cannot see any existing examples of how I could run a command every X minutes.

I can see examples concerning how to time a command but that isn't what I want.

How can I run a command every X minutes in C?

Jack
  • 167
  • 3
  • 13

4 Answers4

4

You cannot do that in standard C99 (that is, using only the functions defined by the language standard).

You can do that on POSIX systems.

Assuming you focus a Linux system, read time(7) carefully. Then read about sleep(3), nanosleep(2), clock_gettime(2), getrusage(2) and some other syscalls(2)... etc...

The issue is to define what should happen if a command is running for more than X minutes.

Read some book about Advanced Linux Programming or Posix programming.

BTW, Linux has crontab(5) and all the related utilities are free software, so you could study their source code.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    C11 added [`thrd_sleep`](http://en.cppreference.com/w/c/thread/thrd_sleep). Unfortunately, [it is not widely supported yet](https://stackoverflow.com/questions/22875545/c11-gcc-threads-h-not-found/22875599#22875599). – 5gon12eder Oct 05 '15 at 19:25
1

You could ask your calling thread to sleep for specified seconds.

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

This conform to POSIX.1-2001.

sleep is a non-standard function. As mentioned here:

  • On UNIX, you shall include <unistd.h>.
  • On MS-Windows, Sleep is rather from <windows.h>
Community
  • 1
  • 1
Rahul Jha
  • 1,131
  • 1
  • 10
  • 25
  • 1
    These two functions are different. They have different names, a different argument and different return codes. – alk Oct 05 '15 at 19:22
  • Yep the functions are different and are placed in seperate header files. One returns int other a void – Rahul Jha Oct 05 '15 at 19:31
0

To do this, and allow other things to happen between calls, suggests using a thread.
This is untested pseudo code, but if you are using Linux, it could look something like this: (launch a thread and make it sleep for 60 seconds in the worker function loop between calls to your periodic function call)

void *OneMinuteCall(void *param);
pthread_t thread0;

int gRunning == 1;

OneMinuteCall( void * param )
{
    int delay = (int)param;
    while(gRunning)
    {
        some_func();//periodic function
        sleep(delay);//sleep for 1 minute
    }
}

void some_func(void)
{
    //some stuff
}

int main(void)
{
    int delay = 60; //(s)
    pthread_create(&thread0, NULL, OneMinuteCall, delay);
    //do some other stuff
    //at some point you must set gRunning == 0 to exit loop;
    //then terminate the thread

    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

As user3386109 suggested, using some form of clock for the delay and sleep to reduce cpu overhead would work. Example code to provide the basic concept. Note that the delay is based on an original reading of the time, (lasttime is updated based on desired delay, not the last reading of the clock). numsec should be set to 60*X to trigger every X minutes.

/* numsec = number of seconds per instance */
#define numsec 3
time_t lasttime, thistime;
int i;
    lasttime = time(NULL);
    for(i = 0; i < 5; i++){     /* any loop here */
        while(1){
            thistime = time(NULL);
            if(thistime - lasttime >= numsec)
                break;
            if(thistime - lasttime >= 2)
                sleep(thistime - lasttime - 1);
        }
        /* run periodic code here */
        /* ... */
        lasttime += numsec;     /* update lasttime */
    }
rcgldr
  • 27,407
  • 3
  • 36
  • 61