You are not logged in.

#1 2015-09-03 06:45:29

lynix
Member
From: Karlsruhe, Germany
Registered: 2008-04-23
Posts: 230

Cron vs. Systemd .timer - strong limitation for inactive sessions

I recently came across something that seems like a very strong limitation, when migrating from Cron to Systemd .timer units:

If I got it right, the equivalent of a regular user's crontab should be .timer units defined in the user instance of systemd (systemctl --user). My problem with this is, that the user instance (afaik) is not running when the user is not logged in, so the timer would not be triggered then. While this might be no problem on desktops, it surely is on servers.

What makes it even worse is the fact that you cannot just convert the user crontabs to system .timer units and have them configured via environment files, e.g.

[Unit]
Description=User crontab (profile %i)
After=network.target

[Service]
Type=simple
EnvironmentFile=/etc/conf.d/crontab-%i
ExecStart=/usr/bin/sh $command
User=$user

[Install]
WantedBy=multi-user.target

This does not work as you cannot use variables in 'User='.

Did I get something wrong? How would you migrate (user) crontabs to .timer units? (or would you at all?)

Last edited by lynix (2015-09-03 06:45:58)

Offline

#2 2015-09-03 07:17:14

rumpelsepp
Member
From: Bavaria
Registered: 2012-11-13
Posts: 105
Website

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

To ensure that systemd --user is running without an active session you just have to linger your user: https://sevenbyte.org/2014/11/15/cron-j … timer.html

It is in the last paragraph, sorry for being not more verbose, just on the phone. smile


Every time I see some piece of medical research saying that caffeine is good for you, I high-five myself. Because I'm going to live forever. -- Torvalds, Linus (2010-08-03).

Offline

#3 2015-09-03 07:32:06

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

Good tip [linger].  Should be added to the wiki if not already there.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2015-09-03 08:08:20

rumpelsepp
Member
From: Bavaria
Registered: 2012-11-13
Posts: 105
Website

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions


Every time I see some piece of medical research saying that caffeine is good for you, I high-five myself. Because I'm going to live forever. -- Torvalds, Linus (2010-08-03).

Offline

#5 2015-09-03 08:13:09

lynix
Member
From: Karlsruhe, Germany
Registered: 2008-04-23
Posts: 230

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

Thanks for the tip with lingering the user smile

But doesn't this run all user-defined units then? Lets say I have a .timer that performs backups, this should run even if I'm not logged in, but a .service starting autossh for a tunnel should only be run if I'm actually logged in on the machine and doing stuff. So how can we distinguish between the .timer and autossh.service?

Offline

#6 2015-09-03 08:29:37

rumpelsepp
Member
From: Bavaria
Registered: 2012-11-13
Posts: 105
Website

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

That's an interesting question; I guess that is not possitble atm. I think I will ask that on the systemd mailing list (unless somebody is faster than me).


Every time I see some piece of medical research saying that caffeine is good for you, I high-five myself. Because I'm going to live forever. -- Torvalds, Linus (2010-08-03).

Offline

#7 2015-09-03 16:52:39

lynix
Member
From: Karlsruhe, Germany
Registered: 2008-04-23
Posts: 230

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

Go ahead, I'm curious to find out what they say to this! smile

Another thing I noticed is the following: having logged out of my Xorg session, the daemons started in the user instance of systemd (e.g. autossh) are not killed. Maybe I have to issue a 'systemctl --user exit' before performing the actual logout.

Offline

#8 2015-09-04 17:05:12

Leonid.I
Member
From: Aethyr
Registered: 2009-03-22
Posts: 999

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

lynix wrote:

Did I get something wrong? How would you migrate (user) crontabs to .timer units? (or would you at all?)

Exactly, why would you? Think of it this way: systemd provides poor-man versions of various tools (cron, syslog, LXC). These versions are meant to be limited and are not intended to be useful in complex situations ("complex" is of course a matter of perspective). In your case, just use cron and don't waste time on systemd-timers...


Arch Linux is more than just GNU/Linux -- it's an adventure
pkill -9 systemd

Offline

#9 2015-09-04 18:35:14

lynix
Member
From: Karlsruhe, Germany
Registered: 2008-04-23
Posts: 230

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

Leonid.I wrote:

Exactly, why would you? Think of it this way: systemd provides poor-man versions of various tools (cron, syslog, LXC). These versions are meant to be limited and are not intended to be useful in complex situations ("complex" is of course a matter of perspective). In your case, just use cron and don't waste time on systemd-timers...

With all respect, I strongly disagree: journald, in my opinion, is far more powerful than syslog-ng, with all its meta-information and flexible handling of volatile system logs. systemd-timers, apart from the limitation we are talking about, are as powerful as fcron concerning time-based triggering already, and I don't think development will stop here. I can't say anything about LXC by now, as I've never used them.

Time will tell what's on systemd creator's minds, but I wouldn't say these mechanisms are meant to be poor-man versions...

Offline

#10 2015-09-04 20:05:44

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Cron vs. Systemd .timer - strong limitation for inactive sessions

lynix wrote:

But doesn't this run all user-defined units then? Lets say I have a .timer that performs backups, this should run even if I'm not logged in, but a .service starting autossh for a tunnel should only be run if I'm actually logged in on the machine and doing stuff. So how can we distinguish between the .timer and autossh.service?

If you install everything as a dependency of the default.target, then yes, they will all run. A basic setup for this scenario could define a login.target. Then make this start when you log in (could just be `systemctl --user start login.target` in a login script) and go back to only the non-login units at the end of your session with

$ systemctl --user isolate default.target

Offline

Board footer

Powered by FluxBB