wait for a time
I have a requirement to start my application at certain time. I don’t want to be put in the corn job. My executable is an application and like to start on 2011-Jan-20 So I have to run it as ./app –date 2011-Jan-20
Here problem is, how I will calculates the time difference from current and date supplied in command line 开发者_运维问答option.
I don’t want to write won function. Is there any in build function are available for such type of time difference. ( c and Linux)
I know you're expecting a C
answer but this might interest you:
Since you're on linux, the system provides already an efficient way to schedule ponctual tasks: at
In your case, an user that would like to run his task on the 20.01.2011 at 8AM, would just type:
echo "./app" | at 08:00 20.01.2011
The task will be run using the credentials of the user. Note that at
also accept relative time directives such as at now +1 day
. It is a powerful tool which ships with most Linux distributions by default.
The list of scheduled jobs can be get using:
atq
And you can even remove scheduled jobs using:
atrm
Hope this helps.
You can calculate the difference between the start time and now in milliseconds and then wait for that many milliseconds by passing that number as a timeout argument to select()
or epoll()
.
To calculate the difference, one way is to first convert your date string to struct tm
using strptime()
and then pass it to mktime()
which is going to give you a number of seconds since unix epoch 1970-01-01 00:00:00. Then get the current time by using gettimeofday()
or clock_gettime()
, they also report time passed since unix epoch. Convert the start time and the current time to seconds and subtract the values.
精彩评论