Systemd service, Working Directory not change the directory

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.


Solution 1:

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.

Solution 2:

I found relative paths work, but you're using an absolute path for ExecStart. It isn't looking for /home/someuser/somescript.sh, it's looking for /somescript.sh which is looking for the file under the root directory. That simply isn't where the file is, so it fails.

You can probably get it working by changing ExecStart=somescript.sh


From: https://www.freedesktop.org/software/systemd/man/systemd.service.html

For each of the specified commands, the first argument must be either an absolute path to an executable or a simple file name without any slashes.