• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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
0
izloch2
Asked:
izloch2
1 Solution
 
Duncan RoeSoftware DeveloperCommented:
You can run the program once a day from cron - man crontab for more on this approach.
Or are you saying the program runs all the time but has to do this task once a day? There's lots of ways of doing that too - post again if that's what you need.
0
 
izloch2Author Commented:
thank you! and what are the other ways?
0
 
Duncan RoeSoftware DeveloperCommented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
ahoffmannCommented:
the general way in Unix would be a script (or program) called by cron
0
 
NopiusCommented:
I aggree with ahoffman, the better way is to use cron.
0
 
Duncan RoeSoftware DeveloperCommented:
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.
0
 
ahoffmannCommented:
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 :-))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now