Ubuntu – Systemd service, Working Directory not change the directory

18.04servicessystemd

I created this script:

[Unit]
Description=test

[Service]
WorkingDirectory=/home/someuser
ExecStart=/somescript.sh

Restart=always
RestartSec=10

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=autodeploy

Environment=NODE_ENV=production PORT=1494

[Install]
WantedBy=multi-user.target

But when I'm running it, it says:

Process: 8986 ExecStart=/somescript.sh (code=exited, status=203/EXEC)

I understood that this message means the script won't found…
Why Working Directory not working for me?

Thanks.

Best Answer

The systemd WorkingDirectory= setting defines on which directory the service will be launched, same as when you use cd to change a directory when you're working in the shell.

That doesn't mean that all the other paths (including that from ExecStart=) will now be relative to it, so you still need to fully specify the path to your script in that directive:

ExecStart=/home/someuser/somescript.sh

Perhaps you were thinking of the RootDirectory= directive instead? That directory uses the chroot command to switch the root of the filesystem seen by the process by the directory you specify, so from your use of / for the location of the script, that looks like maybe what you wanted... However, using RootDirectory= requires that you have a system image, with binaries and libraries under it. Like, you need to have a /bin/sh to run your shell script, and a /lib with a libc, etc. Typically you can't just use RootDirectory= to just about any directory that you like...

So my advice here in order to fix the issue you're seeing is to just update the ExecStart= to list the full path to your script.

Related Question