0

There's a script which has to be run by a systemd service's ExecStartPre. What would be the proper path to put this script into and how to specify it in the unit? Currently using paths such as these fail (the service won't start due to bad unit file):

  • script.sh
  • ./script.sh
  • /etc/systemd/system/script.sh
  • /usr/local/sbin/script.sh

Here's the error I get in the status:

Process: 8679 ExecStartPre=/usr/local/sbin/script.sh (code=exited, status=203/EXEC)

And here's the script:

#!/bin/bash
if [ ! -e /var/log/myservice ];
        then
                echo "myservice's log directory doesn't exist. creating it."
                mkdir /var/log/myservice
fi
aderchox
  • 691

1 Answers1

2

Since you don’t need to change your script, the advice in this answer applies: the script should go in /usr/local/sbin. It also needs to be executable. Your ExecStartPre would be

ExecStartPre=/usr/local/sbin/script.sh

However you don’t need a script at all to create a directory:

ExecStartPre=mkdir -p /var/log/myservice

Better yet, use LogsDirectory=, which will create the directory if necessary and ensure it’s available inside the sandbox if the service is sandboxed:

LogsDirectory=myservice
Stephen Kitt
  • 434,908
  • I thought p was for creating the parents, but I didn't know about -p, --parents no error if existing.... Great! Thanks. – aderchox Sep 08 '20 at 08:18
  • The keen eyed can spot the systemd way of doing this at https://unix.stackexchange.com/q/567317/5132 . (-: – JdeBP Sep 08 '20 at 08:45
  • Ah yes, thanks @JdeBP, I thought there was something like that but couldn’t find it. – Stephen Kitt Sep 08 '20 at 08:56