Whoami Whoami - 1 month ago 9
Linux Question

cronjob vs daemon in linux. When to use?

There are advantages making a process daemonized, as it it detached from the terminal. But the same thing also can be achieved by cron job as well. [ Kindly correct me if not ]

What is the best requirement with which i can differentiate the scenarios when to use cronjob or daemon process?

Answer

In general, if your task needs to run more than a few times per hour (maybe <10 minutes) you probably want to run a daemon.

A daemon which is always running, has the following benefits:

  • It can run at frequencies greater than 1 per minute
  • It can remember state from its previous run more easily, which makes programming simpler (if you need to remember state) and can improve efficiency in some cases
  • On an infrastructure with many hosts, it does not cause a "stampedeing herd" effect
  • Multiple invocations can be avoided more easily (perhaps?)

BUT

  • If it quits (e.g. following an error), it won't automatically be restarted unless you implemented that feature
  • It uses memory even when not doing anything useful
  • Memory leaks are more of a problem.

In general, robustness favours "cron", and performance favours a daemon. But there is a lot of overlap (where either would be ok) and counter-examples. It depends on your exact scenario.