News for hackers. Stuff that matters.
Re:Crontab.guru – cron schedule expression editor by betaby on Thursday October 03, 2019 @05:29PM (#21152432)
Better use systemd as a reliable cron replacement https://wiki.archlinux.org/index.php/Systemd/Timers
Re:Crontab.guru – cron schedule expression editor by alxlaz on Friday October 04, 2019 @04:23AM (#21155823)
Not that I have a problem with systemd, which I don't hate and I use extensively (both in my customers' products and on my machine because it's the default init system of the distro I use), but this thread illustrates a lot of why a lot of pre-systemd Linux users just don't dig it.

Every time someone asks "why should I use systemd instead of cron" they get these (technically correct -- the best kind of correct!) answers about how systemd timers allow you to attach jobs to cgroups, define dependencies, test unit files separately vs. changing crontabs for 1 minute ahead (which I don't think is a "cron idiom" -- I've certainly never done i -- but we all have our weird hacks), and how it has built-in jitter options and per-job environment settings and whatnot.

These are all very relevant for real-life workloads of super advanced cloud systems, I'm sure. (Which I'm not saying in a derogatory way. I haven't written back-end code in like 12 years now, the things that are happening there today blow my mind. I know these are complex systems so I'm pretty sure they have complex problems which have complex solutions.)

But guys, I just need to run a bash script every day at 2 AM. 99% of us just need to run a bash script every day at 2 AM. This has worked reliably in cron since practically forever. I have grumpily learned the new way of doing it but I can't really justify the time I've put into it, nor the (very extensive) troubleshooting that was involved in it. I just woke up one morning and sat down in front of a Linux machine and I found out that there's a new way to run things at 2 AM.

If you want to woo me with cool systemd-timers features, tell me that it finally has an equivalent to cron's MAILTO, so that I don't have to write yet another systemd unit file that wraps a script written by me if I want to get a notification when a job has failed, because somehow that hasn't made its way into the "everything but the kitchen sink" list which otherwise includes things like attaching jobs to cgroups.

Re:Crontab.guru – cron schedule expression editor by yrro on Friday October 04, 2019 @06:42AM (#21156399)
I suppose it would be quite nice to have a MailTo= directive in a service. When the service stops, systemd could retrieve whatever it logged to the journal and mail it to the specified address.

Until such a feature is implemented, you could try:

ExecStart=/bin/bash -c 'shopt -o pipefail; mycommand | mail -s [email protected]'

Certainly not as convenient as MailTo=, but a lot less complicated than using OnFailure= with a separate service that tries to mail you the output.

Re:Crontab.guru – cron schedule expression editor by alxlaz on Friday October 04, 2019 @10:02AM (#21157778)
Huh. I never thought of that, although it was so damn obvious (I feel twice as stupid now that I realize I do a version of that on my machine, albeit not in order to send mail). I guess that's what I get for going all "Arch Wiki will you please just show me how you kids are doing it today and leave me be" on it.

But to be honest, not having to do <shell> -c <exec this or send me an email> ever again is one of the reasons why we're doing the whole systemd thing, and one of its big promises. If it comes to writing shell mantras again, I might as well use vixie cron (or one of its descendants) and benefit from 30+ years of bugfixes across more Unices than I can name, plus the acquired wisdom of the Interwebs.

Edit: plus... does this still allow you to get the other goodies, like being able to specify environment variables for a specific job?

Re:Crontab.guru – cron schedule expression editor by yrro on Friday October 04, 2019 @10:09AM (#21157846)
Not sure I follow about the environment variables.

As for 'exec this or send me an email' -- I would also much prefer to see this be done by systemd itself. Fortunately bash's 'pipefail' feature plus bsd-mailx's '-s' flag make this reasonably quick and sane; I wouldn't even bother to attempt it if I were stuck with POSIX sh or if the mail command lacked the logic to not send the mail if stdin is empty.

Re:Crontab.guru – cron schedule expression editor by alxlaz on Friday October 04, 2019 @01:20PM (#21159808)
> Not sure I follow about the environment variables.

I thought that environment variable passed via the Environment= options might not be correctly set, but it seems they are :-).

Re:Crontab.guru – cron schedule expression editor by quleap on Friday October 04, 2019 @01:27PM (#21159876)
Try systemd-cron https://github.com/systemd-cron/systemd-cron. Best of both worlds: simplicity of crontab and manageability of systemd. And it comes with built-in MAILTO support.
Re:Crontab.guru – cron schedule expression editor by JetSpiegel on Monday October 07, 2019 @05:48AM (#21179026)
You can use systemd-run with the --calendar option for one-off timers, it even supports the cron syntax.
Re:Crontab.guru – cron schedule expression editor by arminiusreturns on Thursday October 03, 2019 @06:03PM (#21152736)
One more thing systemd tries to take over. Do not want.

Instead, try GNU mcron (guile-based).

"The mcron program represents a complete re-think of the cron concept originally found in the Berkeley and AT&T unices, and subsequently rationalized by Paul Vixie. The original idea was to have a daemon that wakes up every minute, scans a set of files under a special directory, and determines from those files if any shell commands should be executed in this minute.

The new idea is to read the required command instructions, work out which command needs to be executed next, and then sleep until the inferred time has arrived. On waking the commands are run, and the time of the next command is computed. Furthermore, the specifications are written in scheme, allowing at the same time simple command execution instructions and very much more flexible ones to be composed than the original Vixie format. This has several useful advantages over the original idea. (Changes to user crontabs are signalled directly to mcron by the crontab program; cron must still scan the /etc/crontab file once every minute, although use of this file is highly discouraged and this behaviour can be turned off). "

https://www.gnu.org/software/mcron/manual/mcron.html#Introdu...

Re:Crontab.guru – cron schedule expression editor by michaelmrose on Thursday October 03, 2019 @06:37PM (#21153000)
I love the idea and like lisp but the lisp syntax seems worse than both cron syntax and natural English.

Given the narrowness of the domain is there a good reason not to use English?

Re:Crontab.guru – cron schedule expression editor by arminiusreturns on Thursday October 03, 2019 @11:24PM (#21154521)
You'd have to ask the devs, but I think lispy people tend to live in a bit of a lisp bubble. I know I sorta do with elisp because of emacs.
Re:Crontab.guru – cron schedule expression editor by jordanthoms on Thursday October 03, 2019 @08:53PM (#21153874)
The cron syntax is used by a bunch of other things as well, e.g k8s cronjobs so sites like this are still useful even if you aren't using a traditional cron
Re:Crontab.guru – cron schedule expression editor by pnako on Friday October 04, 2019 @04:13AM (#21155770)
It's only supported on Lennax though.

If you use *BSD, Jenkins, and everything else you use the standard cron syntax.

Re:Crontab.guru – cron schedule expression editor by tannhaeuser on Friday October 04, 2019 @04:39AM (#21155889)
> Lennax

I'm so going to steal this

giving gold

Re:Crontab.guru – cron schedule expression editor by fractalf on Thursday October 03, 2019 @05:36PM (#21152512)
And exacly why is that better than cron?
Re:Crontab.guru – cron schedule expression editor by cripblip on Thursday October 03, 2019 @06:21PM (#21152887)
Define a unit and run it individually to troubleshoot vs modifying the crontab for 1 min ahead Logging in journalctl Built in jitter options to spread tasks across a window in a large fleet Ability to use the unit as a dep for other things Instrumented in systemctl list-timers Store config in user home dir (I think ) for tasks owned by a user
Re:Crontab.guru – cron schedule expression editor by gorgoiler on Thursday October 03, 2019 @09:05PM (#21153930)
These aren’t really better, they are just different ways of doing things the old fashioned way.

(e.g. put all your logic in one script, test it with “env -“, lock exclusively with flock if you need it, or whatever else you have to hand in your language of choice.)

Re:Crontab.guru – cron schedule expression editor by cripblip on Thursday October 03, 2019 @09:23PM (#21154012)
:shrug:, yeah the old fashioned way is possible too (I have to support both), systemd just gives a consistent pattern to make common tasks easy.
Re:Crontab.guru – cron schedule expression editor by simcop2387 on Thursday October 03, 2019 @06:44PM (#21153045)
Also the ability to easily keep the task from running multiple copies at once.
Re:Crontab.guru – cron schedule expression editor by profmonocle on Friday October 04, 2019 @02:04AM (#21155207)
A nice feature of Systemd timers is the ability to say something like "run once per 24 hour period, but I don't care when". The task runs at a random time during the period, which is nice for tasks that hit network services, since you don't get a zillion clients hitting the server at the same time. (i.e. local midnight or UTC midnight.)

Package repo updates and Let's Encrypt renewals are a couple services I've seen use this. There isn't a great way to do this with cron that I know of, besides running some script that sleeps for a random duration.

That said, I still use cron for things that don't need this feature because it's portable and I'm just more familiar with it.

Re:Crontab.guru – cron schedule expression editor by trulyrandom on Thursday October 03, 2019 @05:41PM (#21152551)
Because it integrates nicely with the rest of systemd. If you use systemd, there's no real reason to also have a crond running.
Re:Crontab.guru – cron schedule expression editor by FillardMillmore on Thursday October 03, 2019 @06:14PM (#21152826)
I think both have their use cases. For quick and dirty things (like manual logging/system performance), cron works just fine and is quicker to set up. For tasks that require dependencies or have to happen at very specific points in time (like immediately after the network services are started), systemd is more suited to the task.

There are of course quite a few people who dislike systemd for justifiable reasons (but I doubt it receives the disdain that SELinux does), here's a starting point:

https://ihatesystemd.com/

That said, I very much like systemd and I hope it isn't going anywhere. I think it's improved on a lot of things from the SysVinit days and I hope and believe that its shortcomings will be addressed and improved in the future.

Re:Crontab.guru – cron schedule expression editor by theamk on Thursday October 03, 2019 @08:41PM (#21153829)
My problem with cron for short tasks is that pretty often I forget to add logging and/or locking. And then machine runs out of resources because of all the parallel jobs, and there aren’t even any logs.
Re:Crontab.guru – cron schedule expression editor by zaphirplane on Thursday October 03, 2019 @06:19PM (#21152868)
I so much more prefer people to respond with reasons than say you should use abc, why, because it’s better
Re:Crontab.guru – cron schedule expression editor by arcticfox on Thursday October 03, 2019 @07:32PM (#21153360)
I've also used crontab.guru a million times, precisely 0 for crond itself. Tons of systems use the same syntax.

So the super dismissive "systemd is better" response is kind of missing the point anyways, even with any type of legitimate justification.

Re:Crontab.guru – cron schedule expression editor by hartator on Thursday October 03, 2019 @05:43PM (#21152566)
except systemd will be replaced by something else soon whereas cron has been around for a while.
Re:Crontab.guru – cron schedule expression editor by danellis on Thursday October 03, 2019 @05:52PM (#21152637)
What is systemd being replaced by?
Re:Crontab.guru – cron schedule expression editor by hartator on Thursday October 03, 2019 @05:59PM (#21152685)
systemd vs initd vs upstart. I am pretty sure systemd will be gone soon too as well. Syntax is harder than upstart for example.
Re:Crontab.guru – cron schedule expression editor by gtirloni on Thursday October 03, 2019 @06:46PM (#21153065)
I'm sorry, that ship has sailed. systemd is more than just a init system at this point. Anyway, I'm also curious about what is replacing it soon.
Re:Crontab.guru – cron schedule expression editor by tyingq on Friday October 04, 2019 @08:02AM (#21156713)
There is an "on boot" parameter which may be handy for things that should run both periodically, and at system start. Narrow use case, but it happens.
Re:Crontab.guru – cron schedule expression editor by jjjbokma on Friday October 04, 2019 @08:14AM (#21156805)
Some cron implementations do have @reboot (and several others).
Re:Crontab.guru – cron schedule expression editor by emmelaich on Thursday October 03, 2019 @11:38PM (#21154582)
Because `systemctl list-timers` is a nicer output than `crontab -l -u <user>`
Re:Crontab.guru – cron schedule expression editor by betaby on Thursday October 03, 2019 @05:47PM (#21152598)
Restarts, dependencies, various resource limits, etc.
Re:Crontab.guru – cron schedule expression editor by zelly on Thursday October 03, 2019 @05:40PM (#21152543)
because it is Deprecated™
Re:Crontab.guru – cron schedule expression editor by 77pt77 on Thursday October 03, 2019 @09:00PM (#21153908)
Has anyone ever managed to have per user systemd services?

For whatever reason it never works for me, including timers.

Re:Crontab.guru – cron schedule expression editor by stevekemp on Friday October 04, 2019 @02:13AM (#21155244)
Yup. You need to enable the "linger" stuff, so that jobs work when the user isn't around.

But I deploy a lot of services running as specific users and it works well. Good overview here:

https://wiki.archlinux.org/index.php/systemd/User

(You can install global services, and add `user=x`, etc, obviously. But I like to keep things self-contained.)

Re:Crontab.guru – cron schedule expression editor by cripblip on Friday October 04, 2019 @10:38PM (#21163510)
The target names for install are confusingly different (default vs multi-user) , check that when enabling, works really well
Re:Crontab.guru – cron schedule expression editor by chousuke on Friday October 04, 2019 @02:26AM (#21155300)
I suspect that if someone just replaced crond with a thing that generates systemd timers, few people would notice.
Re:Crontab.guru – cron schedule expression editor by hartator on Thursday October 03, 2019 @05:43PM (#21152569)
> Description=Run foo weekly and on boot

I hate this kind of syntax.

Re:Crontab.guru – cron schedule expression editor by danellis on Thursday October 03, 2019 @05:51PM (#21152628)
What syntax? It's free text.
Re:Crontab.guru – cron schedule expression editor by hartator on Thursday October 03, 2019 @06:00PM (#21152701)
Oh you are right.

Well the actual syntax is even worse:

[Timer] OnBootSec=15min OnUnitActiveSec=1w

Re:Crontab.guru – cron schedule expression editor by yrro on Friday October 04, 2019 @06:45AM (#21156412)
What would you prefer?