Executing chdir before starting systemd service

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:

Description=Test of listing CWD.

ExecStartPre=chdir /usr/local



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?

Asked By: Mario


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


to get your script to execute there.


Answered By: Eric Renouf
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.