Link to home
Start Free TrialLog in
Avatar of izloch2
izloch2

asked on

Setting up timer in linux

Hi! I need to write a program that will be running under Linux. The program should check a database once a day and if some criteria are met, it needs to trigger some action. The analog of the similar function in Windows is SetTimer() function – you supply a time interval and a function that will be called. How can I implement the same thing in Linux? The program should be running as a background process? Thx
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of izloch2
izloch2

ASKER

thank you! and what are the other ways?
You can use alarm() and a signal handler. Or gettimeofday() in the main despatcher loop (if your program is structured that way) (i.e. use in conjunction with poll() / select() to set a max timeout value). Or run a separate thread which notifies a semaphore. You always end up calling gettimeofday() and scheduling how long from now to call the function.
Separate program from cron is simplest though.
the general way in Unix would be a script (or program) called by cron
I aggree with ahoffman, the better way is to use cron.
ahoffman, Nopius: if you look back you will see I suggested cron first up. However izloch2 then asked "what are the other ways" which I took to possibly mean he has this program running 24/7 and it has to do this task once a day, so I gave him some options.
Now I come to think of it, even in that scenario his program could be checking every minute or so for a file to exist: the file could be created by "touch" in a cron job then the program would do the business and rm the file.
duncan_roe, my comment was meant as Cc: to your first one ;-)

> ..  asked "what are the other ways" ..
hmm, without providing more information *why* there is a need for something else than cron, it's useless to guess and give hints for reenventing the wheel.
In Unix/Linux you don't need to write program for such simple things like doing something every n whatever, it's still there: cron (a bit rusty, 30 years or so, but still working perfect :-))