You are not logged in.

#1 2014-08-19 16:45:03

Osiris
Member
Registered: 2003-01-18
Posts: 157
Website

systemd: Use instantiated unit with path

Hello,

I have a systemd unit that should do a commit on a specific path. Since there are more then one path / repos, I want to use an instantiated unit for that (the stuff with @ in the unit name):

~/.config/systemd/user % cat git-commit@.service
[Unit]
Description=Auto commit for instance.

[Service]
Type = oneshot
Nice = 10

# git returns 1 if there is nothing to commit
SuccessExitStatus=1

WorkingDirectory = %I  # also tried %i
ExecStart = /usr/bin/sh -c 'echo %i > wd'
ExecStart = /usr/bin/sh -c 'echo %i >> wd'
ExecStart = /usr/bin/sh -c 'echo `pwd` >> wd'
ExecStart = /usr/bin/git add --all .
ExecStart = /usr/bin/git commit -a -m "Automatic commit."

It seems that using %i or %I as working directory does not work. When I do systemctl --user start git-commit@/home/florian/timer.test.service the journal prints:

Aug 19 18:43:01 asaru systemd[375]: Starting Auto commit for instance....
Aug 19 18:43:01 asaru git[4761]: fatal: Not a git repository (or any of the parent directories): .git
Aug 19 18:43:01 asaru systemd[375]: git-commit@-home-florian-timer.test.service: main process exited, code=exited, status=128/n/a
Aug 19 18:43:01 asaru systemd[375]: Failed to start Auto commit for instance..
Aug 19 18:43:01 asaru systemd[375]: Unit git-commit@-home-florian-timer.test.service entered failed state.

and wd (created in ~) contains

-home-florian-timer.test
-home-florian-timer.test
/home/florian

Any ideas how I can change the working directory to the directory given by the instance name?

Thx!

Last edited by Osiris (2014-08-19 17:01:34)

Offline

#2 2014-08-19 17:00:06

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: systemd: Use instantiated unit with path

This seems pretty good; unfortunately systemd replaces all slashes with '-' but that is by design. I think you'll need a minor wrapper to handle `chdir ${i/-/\/}`

Offline

#3 2014-08-22 06:42:53

Osiris
Member
Registered: 2003-01-18
Posts: 157
Website

Re: systemd: Use instantiated unit with path

I've brought that to the systemd mailing list and learned a lot. Indeed it's possible without a wrapper.

http://article.gmane.org/gmane.comp.sys … evel/22154

Offline

#4 2014-08-23 06:57:33

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: systemd: Use instantiated unit with path

With the summary from the response on that post being; %I works fine

Offline

#5 2014-08-23 12:50:39

Osiris
Member
Registered: 2003-01-18
Posts: 157
Website

Re: systemd: Use instantiated unit with path

Well, I had some problems using %I with enabling timer units. I think the canonical way is using %f is properly escaped path, as outlined by Lennart. systemd-escape is not in the Repos yet though escaping could be done by hand easily.

Offline

Board footer

Powered by FluxBB