177

Question: Can I kick off a process with systemd and assign that process a working directory of my choosing?

I have a service that I want to start with systemd. When that service is being started, I want to be able to assign it a current working directory. I know how to do this if I was using init, but I'm having trouble with systemd.

Here's what I've been trying to get working.

My Service

I created a simple utility ("listdir"), written in Python, and placed in /opt/bin/listdir:

#! /usr/bin/python

import os

print 'Current working directory: %s' % (os.getcwd())

My Configuration File

I then created a listdir.service file for systemd and placed it here: /lib/systemd/system/listdir.service:

[Unit]
Description=Test of listing CWD.

[Service]
ExecStartPre=chdir /usr/local
ExecStart=/opt/bin/listdir
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

Problem

When I run systemctl start listdir my system log records the root directory ("/") as the current working directory. Of course, I expected /usr/local as the current directory, since I thought ExecStartPre would change directories before starting the process.

Obviously, I'm imagining that systemd would work something like a shell script (even though I know it isn't a shell script). Can someone give me an idea of what I should be doing? Is it even possible to set a working directory using systemd? Thanks!


Edit: My system log is reporting an error. (I just noticed.)

Executable path is not absolute, ignoring: chdir /usr/local 

So, chdir is a shell command, and not an executable itself. Okay. But is there still some way for me to change directories using systemd?

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
Mario
  • 1,965
  • 2
  • 12
  • 8

1 Answers1

323

On systemd >= 227 you should be able to use:

[Service]
WorkingDirectory=/usr/local

to get your script to execute there.

(DOCS)

Stephen Rauch
  • 4,209
  • 14
  • 22
  • 32
Eric Renouf
  • 18,141
  • 4
  • 49
  • 65
  • Thank you! That's exactly it. I'm sorry I didn't come across that on my own. (I can accept your answer in 8 minutes.) – Mario May 05 '15 at 20:15
  • 5
    How did you find this? It's not mentioned anywhere in [the documentation](https://www.freedesktop.org/software/systemd/man/systemd.service.html)! – jameshfisher Oct 17 '16 at 15:33
  • 3
    @jameshfisher it's in [this part of the documentation](https://www.freedesktop.org/software/systemd/man/systemd.exec.html) – Eric Renouf Oct 17 '16 at 15:36
  • 4
    @EricRenouf aha, they should just duplicate those shared options in the service documentation – jameshfisher Oct 18 '16 at 10:52
  • 3
    So just for clarity, would this go in the `[Service]` section? – dthor Dec 20 '16 at 01:32
  • 1
    Yup, realized my mistake and changed it, sorry for the ninja-edit! But yes, thanks, `[Service]` is the correct section. – dthor Dec 20 '16 at 01:55
  • CentOS 7 seems to be using 219, do you know if this feature was backported? – Bruno Medeiros Aug 29 '17 at 20:14
  • 4
    Answering myself, it worked on latest CentOS 7, despite of the version. – Bruno Medeiros Aug 29 '17 at 20:30
  • [systemd.exec(5)](https://www.freedesktop.org/software/systemd/man/systemd.exec.html) says "The execution specific configuration options are configured in the `[Service]`, `[Socket]`, `[Mount]`, or `[Swap]` sections, depending on the unit type.", in addition to options specific to `[Service]` section mentioned [here](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Options). – Jeremy Kao Oct 18 '18 at 08:15