57

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.

7
  • 6
    There is a / in front of somescript.sh. I don't suppose you put somescript.sh in the root of the filesystem?
    – zwets
    Aug 7, 2018 at 9:53
  • I put it inside /home/someuser. It's needs to run in this directory. I tried to do it with WorkingDirectory but it can't find the script.
    – Roi Amiel
    Aug 7, 2018 at 10:01
  • 2
    If you put it in /home/someuser, then tell the system that it is there. Look closely at ExecStart=/somescript.sh: where does this say the script is? (Hint: where is /home located?)
    – zwets
    Aug 7, 2018 at 10:14
  • I need the service to run it from the original directory only (/home/someuser/somescript.sh will not work for me), I tried to use WorkingDirectory to change the directory before executing it. Do you have some idea how can I do it?
    – Roi Amiel
    Aug 7, 2018 at 10:32
  • I really don't get what you want? This works: ExecStart=/home/someuser/somescript.sh and is the way to do it; otherwise there might be another error not related to the service.
    – Rinzwind
    Aug 7, 2018 at 10:39

3 Answers 3

76

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.

1
  • 1
    But what does ExecStart=./somescript.sh do?
    – Joshua
    Jan 17 at 0:33
30

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.

5
  • 4
    Hilariously, this is actually the correct answer to the question. While @filbranden's answer is excellent, it's not actually the answer to the question. Have an upvote infernalrapture, you deserve it.
    – jmc
    May 18, 2021 at 12:00
  • ./ did not work for me in Debian Bullseye Aug 24, 2021 at 7:55
  • 3
    Relative paths do not work for the binary that Systemd runs. However, that binary, once running, can do whatever it wants with the arguments that are passed to it, like treat them as relative paths. Sep 9, 2021 at 19:22
  • 1
    Thanks for the feedback @CameronTacklind and @forresthopkinsa. I corrected this answer by removing ./ from ExecStart and adding a quote from documentation and a link to the reference. ☮️❤️🌈🧘🏽 Jan 18, 2022 at 18:43
  • 1
    If you don't use an absolute path, then it will search in a at-compiled-time predefined path (which you can query with systemd-path search-binaries-default). So even if you set Environment=PATH=..., if the command is not in any of the predefined directories, systemd will not found it. See freedesktop.org/software/systemd/man/…
    – Pablo
    Feb 2, 2023 at 19:23
4

This also works:

WorkingDirectory=/home/someuser
ExecStart=/bin/bash -c './somescript.sh'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .