Cronjob vs daemon on linux. When to use?

There are advantages that make the process disassembled as it disconnects from the terminal. But the same can also be achieved with cron. [Please correct me if not]

What is the best requirement that I can distinguish between scripts when using a cronjob or daemon process?

+50
linux
Oct 19 '12 at 13:39
source share
4 answers

In general, if your task needs to be completed more than a few times per hour (possibly less than 10 minutes), you probably want to run the daemon.

A daemon that always works has the following advantages:

  • It can operate at frequencies greater than 1 per minute
  • It easier to remember the state from its previous launch, which simplifies programming (if you need to remember the state) and can increase efficiency in some cases
  • In an infrastructure with many hosts, it does not produce the effect of a stamped herd.
  • Multiple calls can be easily avoided (maybe?)

BUT

  • If it shuts down (for example, after an error), it will not restart automatically unless you have implemented this function
  • He uses memory even when he does nothing.
  • Memory leaks are more of a problem.

In general, robustness favors cron, while productivity favors the demon. But there are many overlaps (where it will be good somewhere), and counter examples. It depends on your exact scenario.

+41
Oct 19 '12 at 21:13
source share
— -

The difference between cronjob and the daemon is the execution time interval.

A cronjob is a process that runs once in a while. An example of a cronjob is a script that from time to time deletes the contents of a temporary folder or a program that sends push notifications every day at 9.00 to several devices.

While the demon - This is a process that disconnects from any user, but does not restart if it comes to an end.

+11
Oct 19 '12 at 13:40
source share

If you need a service that is constantly available to others, you need to start the daemon. This is a rather difficult programming task, because the daemon must be able to constantly communicate with the world (for example, while listening to a socket or TCP port), and it must be written to process each task without leakage or even blocking resources for a long time.

In contrast, if you have a specific task, the description of which can be determined well enough in advance and which can act automatically without additional information and is self-sufficient, then the cron task, which runs the task periodically, can be completed. This is much easier to design, because you only need a program that runs once for a limited time and then terminates.

In a nutshell: A demon is the only process that runs forever. The cron task is a mechanism that allows you to periodically start a new, short-lived process.

+3
Oct. 19
source share

The daemon can use its durability in caching, postpone writing to disk, or participate in long sessions with the client.

The daemon should also be free of memory leaks, as they can accumulate over time and cause problems.

+1
Oct 19 '12 at 13:45
source share



All Articles