TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Cron in production is a double-edged sword

104 pointsby neo2001over 9 years ago

25 comments

vidarhover 9 years ago
Most of what he&#x27;s writing about, and much more, is made substantially easier with systemd timers.<p>E.g. want errors to cause e-mails, but everything else to just go to logs? Use a timer to activate a service, and make systemd activate another service on failure.<p>Want to avoid double execution? That&#x27;s the default (timers are usually used to activate another unit, as long as that unit doesn&#x27;t start something that doubleforks, it won&#x27;t get activated twice).<p>(Some) protection against thundering herd is built in: You specify the level of accuracy (default 1m), and each machine on boot will randomly select a number of seconds to offset all timers on that host with. You can set this per timer or for the entire host.<p>And if you&#x27;re using fleet, you can use fleet to automatically re-schedule cluster-wide jobs if a machine fails.<p>And the journal will capture all the output and timestamp it.<p>systemctl list-timers will show you which timers are scheduled, when they&#x27;re scheduled to run next, how long is left until then, when they ran last, how long that is ago:<p><pre><code> $ systemctl list-timers NEXT LEFT LAST PASSED UNIT Sat 2015-10-17 01:30:15 UTC 51s left Sat 2015-10-17 01:29:15 UTC 8s ago motdgen.timer Sat 2015-10-17 12:00:34 UTC 10h left Sat 2015-10-17 00:00:33 UTC 1h 28min ago rkt-gc.timer Sun 2015-10-18 00:00:00 UTC 22h left Sat 2015-10-17 00:00:00 UTC 1h 29min ago logrotate.timer Sun 2015-10-18 00:15:26 UTC 22h left Sat 2015-10-17 00:15:26 UTC 1h 13min ago systemd-tmpfiles-clean.timer </code></pre> And the timer specification itself is extremely flexible. E.g. you can schedule a timer to run x seconds after a specific unit was activated, or x seconds after boot, or x seconds after the timer itself fired, or x seconds after another unit was deactivated. Or combinations.
评论 #10403891 未加载
评论 #10406423 未加载
NightMKoderover 9 years ago
Something we&#x27;ve found to be fairly lightweight (compared to e.g. Chronos), but incredibly featureful is using Jenkins (the CI server) as a cron runner. We use <a href="http:&#x2F;&#x2F;docs.openstack.org&#x2F;infra&#x2F;jenkins-job-builder&#x2F;" rel="nofollow">http:&#x2F;&#x2F;docs.openstack.org&#x2F;infra&#x2F;jenkins-job-builder&#x2F;</a> to configure it at deploy-time so it lives as part of the deploy rather than system config.<p>Here&#x27;s a small list of things we&#x27;re getting out of it:<p>- concurrent run protection (&amp; queue management via <a href="https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Concurrent+Run+Blocker+Plugin" rel="nofollow">https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Concurrent+Run+B...</a> )<p>- load balancing (e.g. max concurrent tasks) and remote execution with jenkins slaves [sounds complicated, but really jenkins just knows how to SSH]<p>- job timeouts. No more hanging jobs.<p>- failure notifications via slack&#x2F;hipchat&#x2F;email&#x2F;whatever. [email only on status change via <a href="https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Email-ext+plugin" rel="nofollow">https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Email-ext+plugin</a> ]<p>- log&#x2F;history management: rotation &amp; compression.<p>- fancy scheduling: e.g. run this job once every 24h, but if it fails keep retrying in 5 minute increments (<a href="https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Naginator+Plugin" rel="nofollow">https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Naginator+Plugin</a> ). You could also use project dependencies for pipelines, but we&#x27;ve been staying away from that.<p>- monitoring: we use the datadog reporter &amp; alert on time since last success. Given how mature Jenkins is, this likely translates to whatever system you&#x27;re using just as well.<p>It&#x27;s worked incredibly well for us. We migrated to Jenkins from crontabs with cronwrap (<a href="https:&#x2F;&#x2F;github.com&#x2F;zomo&#x2F;cronwrap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;zomo&#x2F;cronwrap</a>). We&#x27;re never going back.
评论 #10404776 未加载
评论 #10403169 未加载
评论 #10403707 未加载
评论 #10404736 未加载
nathancahillover 9 years ago
I&#x27;ve been using Dead Man&#x27;s Snitch[0] in production for a few years. It&#x27;s been a life saver. Not affiliated, just a happy customer.<p>[0] <a href="https:&#x2F;&#x2F;deadmanssnitch.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;deadmanssnitch.com&#x2F;</a>
评论 #10402761 未加载
评论 #10402900 未加载
评论 #10403543 未加载
liquidiseover 9 years ago
This is not an argument against cron. It is a demonstration of people not abstracting code. One of the thousands i&#x27;ve come across.<p>Take all of the features he mentions, and abstract the to a launch_from_cron.sh file. Make that file accept a script path as an argument and viola! All of the safety added to cron without the need for code duplication or these massive overhead solutions listed in these comments.
评论 #10405750 未加载
josnyderover 9 years ago
I work for Yelp, and we use cron for purposes similar to those mentioned in this article, mostly synchronizing small bits of configuration or data that we want local to the machine. We&#x27;re heavy Puppet users, and we made a module to assist us in the management of our crons [1]. If you&#x27;re a Puppet shop, I highly recommend checking it out. It provides answers to each of the problems mentioned in the article, often using the same mechanisms. I especially like its integration with Sensu, which we use for monitoring the jobs.<p>We&#x27;ve found that deploying cronjobs onto individual hosts is quite powerful, and helps us fill a niche between configuration management tools (like Puppet) and specialized coprocesses (like Smartstack). We have cronjobs for downloading code deploys, showing Sensu state within the motd, reconfiguring daemons (especially the Smartstack ones), and (of course) cleaning up unused data.<p>Of course, there&#x27;s also the separate problem of scheduling and coordinating tasks across an entire cluster. In most cases we don&#x27;t use our cron daemons for this, although we do have some jobs that run on multiple hosts and enforce mutual exclusion by grabbing a lock in Zookeeper.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;Yelp&#x2F;puppet-cron#puppet-cron" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Yelp&#x2F;puppet-cron#puppet-cron</a>
sebwover 9 years ago
No one mentionned Rundeck: <a href="http:&#x2F;&#x2F;rundeck.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;rundeck.org&#x2F;</a><p>I&#x27;ve been using it for two years now. This has replaced cron on about 200 nodes.<p>Not only it does cron, but also helps deploying artefacts (integrated with Jenkins) through simple forms. We now have ops with 0 experience in Linux deploying code.
评论 #10404079 未加载
zimbatmover 9 years ago
Having local mailboxes in each server is not really useful in a cloud setup with hundreds of machines. But it&#x27;s not a reason to silence the output; something bad might happen and only stdout&#x2F;stderr might give you an anwer of what exactly is going wrong.<p>Instead use <a href="https:&#x2F;&#x2F;github.com&#x2F;zimbatm&#x2F;logmail" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;zimbatm&#x2F;logmail</a>. It&#x27;s a `sendmail` replacement that forwards everything to syslog. Then forward all your syslogs to a central place an you can capture and analyze these messages.
aidenn0over 9 years ago
I use Jenkins instead of cron. I get an rss feed of processes that exited with non-zero, it captures the output but doesn&#x27;t e-mail it to me. This is totally not what it&#x27;s designed for, but it is closer to what I want than cron is.
评论 #10402965 未加载
评论 #10402929 未加载
INTPenisover 9 years ago
The problem isn&#x27;t cron, cron is just a dumb execution tool.<p>The problem is that we don&#x27;t have any way of alerting our monitoring systems from a cron job.<p>This is exactly what I&#x27;ve been implementing, a simple curl API call to our monitoring system when a cron job has run is all that we need. This puts the monitoring of cron into the same sphere as all other monitoring and puts the alert on a webpage where it can be found eventually by our 2nd line or our on-call personnel, instead of in someones mailbox.<p>Edit: And you don&#x27;t need a fancy REST based API for your monitoring system to do this, ye ol&#x27; nagios agent could do it with some hacks.<p>The hard part is having the discipline to fix all your cron jobs in this way, but adding || true is already tantamount to this.
评论 #10403696 未加载
hendryover 9 years ago
Doesn&#x27;t systemd timers <a href="https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Systemd&#x2F;Timers" rel="nofollow">https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Systemd&#x2F;Timers</a> address these issues?
评论 #10403042 未加载
zpideyover 9 years ago
These all seem like issues you&#x27;d run into with any task scheduler. Error emails, overloading a central resource with many tasks. Most of these aren&#x27;t particular&#x2F;limited to cron at all.
评论 #10402817 未加载
ck2over 9 years ago
gee this is a difficult concept<p>how to get cron to only send important emails and not every time it runs<p>you think maybe you should have just used<p><pre><code> &gt; &#x2F;dev&#x2F;null </code></pre> and not<p><pre><code> &gt; &#x2F;dev&#x2F;null 2&gt;&amp;1 </code></pre> why is this a full blog post?
psiorover 9 years ago
Very interesting discussion. I&#x27;ve been doing some related work where I needed to run some tasks in a non-overlapping way and while flock was an initial option I later moved to redis queue (ie rpush &amp; blpop mix) to guarantee a certain (and needed) order of execution. This is mixed with a &#x27;send email in case of error&#x27; check and so far is doing fine, though I&#x27;ll definitely look into Jenkins if I ever feel this current approach proves not to be reliable enough.
ausjkeover 9 years ago
Great read and definitely will keep this in my toolbox, the whole article is explaining why the below good when you need use cron:<p>15 * * * * ( flock -w 0 200 &amp;&amp; sleep `perl -e &#x27;print int(rand(60))&#x27;` &amp;&amp; nice &#x2F;command&#x2F;to&#x2F;run &amp;&amp; date &gt; &#x2F;var&#x2F;run&#x2F;last_successful_run ) 2&gt;&amp;1 200&gt; &#x2F;var&#x2F;run&#x2F;cron_job_lock | while read line ; echo `date` &quot;$line&quot; ; done &gt; &#x2F;path&#x2F;to&#x2F;the&#x2F;log || true
hbogertover 9 years ago
Excessive use of crons is a devops (hate the word) smell. You get reliant on their side-effects and to migrate to other solutions you need enormous amounts of testing and legacy interfaces. The most obvious downside to a cron is the at least 1m interval. On average you are waiting 30s for something which already should be there. Of course it&#x27;s perfect for things like reporting which make sense for certain intervals. Using it for mail queues and stuff.. bad times.
rbcover 9 years ago
Something else. The cron service is a one hit wonder. All it does is schedule. It places responsibility for handling output and setting semaphores for use by other applications to the person who wrote the command called by cron. You can&#x27;t really blame cron if the command&#x2F;script doesn&#x27;t do these things. You just need to look to another type of scheduler&#x2F;batch facility that provides a richer feature set for handling workflow, monitoring and reporting.
pinterfaceover 9 years ago
I&#x27;ve been pretty happy with shush[1], which is a similar script that helps with a lot of this--including randomdelays, locking to avoid overlapping runs, e-mailing only on errors (or other criteria as you see fit), and so forth.<p>[1] <a href="http:&#x2F;&#x2F;web.taranis.org&#x2F;shush&#x2F;" rel="nofollow">http:&#x2F;&#x2F;web.taranis.org&#x2F;shush&#x2F;</a>
zimbatmover 9 years ago
<a href="https:&#x2F;&#x2F;github.com&#x2F;Yipit&#x2F;cron-sentry" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Yipit&#x2F;cron-sentry</a> is also quite nice as a wrapper to capture failing cron jobs and forward them to <a href="https:&#x2F;&#x2F;getsentry.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;getsentry.com&#x2F;</a>
rbcover 9 years ago
CFEngine also provides a scheduling capability that can be used in conjunction with other factors using boolean expressions. Something like &quot;Run at midnight on Saturday if you are a production linux server.&quot; The splaytime parameter can spread out the execution of a command across a cluster based on its name hash.
ilamparithiover 9 years ago
We use <a href="https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Monitoring+external+jobs" rel="nofollow">https:&#x2F;&#x2F;wiki.jenkins-ci.org&#x2F;display&#x2F;JENKINS&#x2F;Monitoring+exter...</a> to monitor the cron jobs.
impostervtover 9 years ago
I just started using AWS Lambda for a cron-like job I needed. It supports scheduling now.
geggamover 9 years ago
Nice blog explaining how someone cant use cron. Doesn&#x27;t mean the rest of us are that incapable.
评论 #10404409 未加载
TerryADavisover 9 years ago
Linux is designed like a 1970&#x27;s mainframe.<p>TempleOS is designed like a C64.<p>I don&#x27;t see cron as useful for a C64 user.
DannoHungover 9 years ago
Is there any good open source distributed scheduler that blends both timer based tasks and event based tasks?<p>Chronos is the only one I&#x27;m aware of, but I don&#x27;t believe it supports event based tasks.
评论 #10402866 未加载
评论 #10402953 未加载
评论 #10403863 未加载
vacriover 9 years ago
Another cron trick is to use chronic&#x2F;cronic before your command. It silences the command except for error states - cron likes to report any text it sees, which you don&#x27;t want for non-error states. It also detects errors better than just assuming all errors happen on STDERR.