I have a systemd service that needs to create a directory in
/run, but otherwise run as a non-root user. From a blog example, I derived the following solution:
[Unit] Description=Startup Thing [Service] Type=oneshot ExecStart=/usr/bin/python3 -u /opt/thing/doStartup WorkingDirectory=/opt/thing StandardOutput=journal User=thingUser # Make sure the /run/thing directory exists PermissionsStartOnly=true ExecStartPre=-/bin/mkdir -p /run/thing ExecStartPre=/bin/chmod -R 777 /run/thing [Install] WantedBy=multi-user.target
The magic is in the 3 lines that follow the comment. Apparently the
ExecStartPre's will run as root this way, but the
ExecStart will run as the specified user.
This has lead to 3 questions though:
- What does the
-do in front of the
/bin/mkdir? I don't know why it's there or what it does.
- When there are multiple
ExecStartPre's in a unit file, are they just run serially in the order that they are found in the unit file? Or some other method?
- Is this actually the best technique to accomplish my goal of getting the run directory created so that the non-root user can use it?