You are not logged in.

#1 2016-01-16 21:26:31

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

Systemd service that only runs between 7PM and 7AM [SOLVED]

I'd like to make a simple systemd user service that runs /usr/local/bin/foo once every 15 min but only between the hours of 7PM and 7AM.  I didn't see a mechanism for this in the man pages but was thinking a service/timer combo.  What do you guys think?  /usr/local/bin/foo will write a pid file to /run/user/1000/foo.pid and will check for such that if it is called twice, no action is taken.

~/.config/systemd/user/foo.service

[Service]
ExecStart=/usr/local/bin/foo up

[Install]
WantedBy=default.target

Last edited by graysky (2016-01-17 15:07:35)


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

Offline

#2 2016-01-16 21:46:09

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

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

Study "CALENDAR EVENTS" in the systemd.time man page.

Offline

#3 2016-01-16 21:56:30

R00KIE
Forum Fellow
From: Between a computer and a chair
Registered: 2008-09-14
Posts: 4,734

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

I may have missed something but I don't see how just writing a pid file will achieve what you need, although that might play a part on what you want to do.

I have an idea of how you can do it but it is a bit convoluted, here goes anyway:

- Have the foo.service and foo.timer files. Set the timer to start foo.service every 15min.
- In foo.service you can a couple of "ConditionPathExists=" (see man systemd.unit), one would check if the pid files does not exist, thus preventing you from running your /usr/local/bin/foo twice, another line would check for the presence of another file, say /run/user/1000/foo.run
- Have one or more extra timers that touch/rm the foo.run file. This part can get tricky if your machine is off at 7AM or 7PM as timers will not run, or if set to persistent you don't know in which order they will run.

I'd say the easiest way would be for your foo program to check the time and decide to run or not, alternatively wrap it in a script to check the time and maybe handle the pid file creation/deletion.

Edit:
Raynman beat me to it. That idea should work and is much simpler than mine, I guess the saying about hammers and nails is true tongue

Last edited by R00KIE (2016-01-16 22:02:29)


R00KIE
Tm90aGluZyB0byBzZWUgaGVyZSwgbW92ZSBhbG9uZy4K

Offline

#4 2016-01-16 22:50:22

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

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

Thanks for the replies, guys.  Let me see if I understand this.

Goal1: Run '/usr/local/bin/foo up' every 5 min between 7AM and 7PM.
Goal2: Run '/usr/local/bin/foo down' after 7AM.

Here is ~/.config/systemd/user/foo.service

[Service]
Type=simple
ExecStart=/usr/local/bin/foo up
ExecStop=/usr/local/bin/whitenoise down

[Install]
WantedBy=default.target

How might the corresponding timer look to run the differential "up" and "down" commands?  Two entries under the [Timer] section with each respective action?

BTW, congrats on your new role serving the community, R00KIE.

EDIT: Alternatively, I could simply hard-code these start/stop times into the script like I did here, but I'd like to learn the system solution.

Last edited by graysky (2016-01-16 22:54:47)


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

Offline

#5 2016-01-16 23:48:55

R00KIE
Forum Fellow
From: Between a computer and a chair
Registered: 2008-09-14
Posts: 4,734

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

If your foo program creates and deletes the pid file by itself then you probably need just the foo.service and foo.timer files. Assuming your foo program runs and exits then maybe something like

foo.service:

[unit]
Description=My foo program
ConditionPathExists=!/run/user/1000/foo.pid

[Service]
Type=simple
ExecStart=/usr/local/bin/foo

If foo already checks for the existence of the pid file before running you don't need the ConditionPathExists line.

Then your foo.timer would be

[Unit]
Description=foo timer

[Timer]
OnCalendar=19,20,21,22,23,0,1,2,3,4,5,6:0,15,30,45

[Install]
WantedBy=multi-user.target

I'm not very good with the calendar events of systemd so you probably want to test this properly before using it. You might be able to abbreviate or tweak the calendar event specification but I suppose that is what Raynman was thinking about.


R00KIE
Tm90aGluZyB0byBzZWUgaGVyZSwgbW92ZSBhbG9uZy4K

Offline

#6 2016-01-16 23:54:28

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

OnCalendar=19,20,21,22,23,0,1,2,3,4,5,6:0/5

See systemd.time man page for explanation of "/"

Edit: change the "5" to "15" if you want to run every 15 mins rather than 5 mins.

Last edited by ukhippo (2016-01-16 23:56:52)

Offline

#7 2016-01-17 13:33:40

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

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

Thanks guys... so to also have it stop at 7AM, using this outlined strategy, I would think that would mean I would need two service files and two timer files, right?

a.service and a.timer to run '/usr/local/bin/foo up'
and
b.service and b.timer to run '/usr/local/bin/foo down'

How could we morph them into the a single service and a single timer?

..I'm starting to think it would be more simple to add the constrain to the script itself and just have the timer ping the service every 5 min around the clock tongue

Last edited by graysky (2016-01-17 13:34:51)


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

Offline

#8 2016-01-17 14:47:34

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

Timers just specify when to run the service; there's no way to specify which times represent the first and last runs, and you want to do different things depending on the time.
So, unless you put logic into the service (or as you suggest have the logic in a script and the service runs that script), you would need two separate timers and services.

Offline

#9 2016-01-17 15:04:06

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

Re: Systemd service that only runs between 7PM and 7AM [SOLVED]

Yes, I thought that might be the case.  I added logic in the script itself to exit if the hour of `date` is not to my liking as the linked code suggests.  The timer I went with just pings the service (no tokens since that is hardcoded into the script) once every 5 min and all is well.  Thanks for the suggestions.

For reference:
service

[Unit]
Description=white noise generator
Wants=whitenoise.timer

[Service]
Type=simple
ExecStart=/usr/local/bin/whitenoise

[Install]
WantedBy=default.target

timer

[Unit]
Description=check that white noise is running every 5 min
PartOf=whitenoise.service

[Timer]
OnCalendar=*:0/5

Last edited by graysky (2016-01-17 15:07:21)


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

Offline

Board footer

Powered by FluxBB