cronjob vs daemon in linux. When to use?

Linux

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

The difference between a cronjob and a daemon is the execution time frame.

A cronjob is a proccess that is executed once in a while. An example of cronjob could be a script that remove the content of a temporary folder once in a while, or a program that sends push notifications every day at 9.00 am to a bunch of devices.

Whereas a daemon is a process running detached from any user, but wont be re-launch if it comes to end.

Solution 3 - Linux

If you need a service that it permanently available to others, then you need to run a daemon. This is a fairly complicated programming task, since the daemon needs to be able to communicate with the world on a permanent basis (e.g. by listening on a socket or TCP port), and it needs to be written to handle each job cleanly without leaking or even locking up resources for a long time.

By contrast, if you have a specific job whose description can be determined well enough in advance, and which can act automatically without further information, and is self-contained, then it may be entirely sufficient to have a cron job that runs the task periodically. This is much simpler to design for, since you only need a program that runs once for a limited time and then quits.

In a nutshell: A daemon is a single process that runs forever. A cron job is a mechanism to start a new, short-lived process periodically.

Solution 4 - Linux

A daemon can take advantage of it's longevity by caching state, deferring disk writes, or engaging in prolonged sessions with a client.

A daemon must also be free of memory leaks, as they are likely to accumulate over time and cause a problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWhoamiView Question on Stackoverflow
Solution 1 - LinuxMarkRView Answer on Stackoverflow
Solution 2 - LinuxtomahhView Answer on Stackoverflow
Solution 3 - LinuxKerrek SBView Answer on Stackoverflow
Solution 4 - LinuxPaul BeckinghamView Answer on Stackoverflow