[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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