Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cronjob vs daemon in linux. When to use? [closed]

Tags:

linux

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?

like image 954
Whoami Avatar asked Oct 19 '12 13:10

Whoami


People also ask

Do cron jobs run when terminal is closed?

When your computer is shut down (or the cron daemon is otherwise not running), cron jobs will not be started. If you have jobs that you would like to run after the fact during those times when the computer is shut down, use anacron.

Is a cron job a daemon?

Scheduling Recurring Tasks on Linux Using CronCron is a daemon used to schedule any kind of task you can imagine. It is useful to send out emails on system or program statistics, do regular system maintenance, make backups, or do any task you can think of. There are similar programs on other Operating Systems.

Do cron jobs run when computer off?

1 Answer. Show activity on this post. cron jobs run while the computer is on & not sleeping, so it will run in situations 1 and 2. If the computer is off or asleep at the job's scheduled time, it does not do any sort of catch-up run later when the computer restarts/wakes up; therefore, it will not run in situation 3.

When would you use a cron job?

Cron Jobs are used for scheduling tasks to run on the server. They're most commonly used for automating system maintenance or administration. However, they are also relevant to web application development. There are many situations when a web application may need certain tasks to run periodically.


2 Answers

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.

like image 72
MarkR Avatar answered Sep 23 '22 23:09

MarkR


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.

like image 22
tomahh Avatar answered Sep 23 '22 23:09

tomahh