You are not logged in.

#1 2016-08-01 23:40:49

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,909
Website

Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

Is it possible to use a systemd timer unit to start and stop a service unit according to set times of the day?  In my case, foo.service is a forking type if that matters... I can do this using cron, but it would be cool to learn how to do it with systemd natively if possible.

# start at 7 AM
* 7 * * * systemctl start foo.service
# stop at 5 PM
* 17 * * * systemctl stop foo.service

The syntax of the timer with differential commands isn't clear to me even after consulting `man systemd.time` and `man systemd.timer`.

Last edited by graysky (2016-08-02 22:39:14)


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

Offline

#2 2016-08-01 23:46:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,631
Website

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

Probably.  Have you tried?

If you haven't tried it, don't ask here, try it.

If you have tried it, did it work?  If not, what went wrong?

As is this really isn't a sys admin question.  Moved to NC.

EDIT: given your edit, I'll hold off on the move, but it's still questionable.  The only thing I'm not 100% sure on is whether you can have the ExecStart command in a service file be a call to systemctl itself.  I don't see why not, but I've never personally done it.  If it works, then the answer to your question is simply "Yes".

I have no idea what you mean by the "syntax of differential commands".  You have one timer that starts the service at an OnCalendar time, and another one that stops the service at an OnCalendar time - that's it.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-08-01 23:52:55

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,909
Website

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

@Trilby - I have used timers to run (start) a service but never to stop or to bracket a time window with two different actions: one being start and the other being stop, to your confusion about my "syntax of differential commands."  Perhaps you're right and the solution is not a single timer but two timers... this is core of my question to those more experienced with the init system.

EDIT: I actually don't see anything in the systemd.timer manpage about stopping corresponding unit, only activating hmm

Last edited by graysky (2016-08-01 23:54:21)


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

Offline

#4 2016-08-01 23:59:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,631
Website

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

No, there is not (AFAIK) anything in timers to stop the service.  But that's why I said two timers.  One of them which would do the following:

ExecStart=/usr/bin/systemctl stop foo


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2016-08-02 21:13:59

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,297

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

There is a way to use a timer to stop a service without the 'systemctl stop' command:
the stopping timer starts another service which conflicts with the one to stop, using the 'Conflicts=foo.service'.

If a unit has a Conflicts= setting on another unit, starting the former will stop the latter and vice versa.

Offline

#6 2016-08-02 21:39:44

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,909
Website

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]


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

Offline

#7 2016-08-02 21:53:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,631
Website

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

Using Conflicts is a better method.

@Graysky, so you are posting a link to where you cross posted this and got the same answer?  Is this a prelude to marking the thread as solved?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2016-08-02 22:41:06

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,909
Website

Re: Emulate 2 cron tab entries: start/stop service unit natively? [SOLVED]

@Tri - Yes, wanted to close the loop for future searchers to have as much info as possible.  Thanks for the replies, all.


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

Offline

Board footer

Powered by FluxBB