Failed at step EXEC spawning: No such file or directory

linuxpythonservicessystemdUbuntu

Given the following one-shot service, enabled at boot,

[Unit]

Description = My Server Daemon
Documentation = https://etcetc.org

Wants = network-online.target getty.target NetworkManager-wait-online.service isc-dhcp-server.service xinetd.service smbd.service
After = network-online.target getty.target NetworkManager-wait-online.service isc-dhcp-server.service xinetd.service smbd.service

[Service]

Type = oneshot

RemainAfterExit = yes

User = foouser

PIDFile = /run/myd.pid

WorkingDirectory = /home/foouser/my-server/bin

ExecStart = /home/foouser/my-server/bin/myd
ExecStop = /home/foouser/my-server/bin/abort.sh

TimeoutStopSec = 5

[Install]

WantedBy = multi-user.target

It actually fails to start. Filtering /var/log/syslog

systemd[1]: myd.service: Failed at step CHDIR spawning /home/foouser/my-server/bin/my: No such file or directory

While journalctl --unit=myd.service agrees

systemd[1]: myd.service: Main process exited, code=exited, status=200/CHDIR

So, since /home/foouser/my-server/bin/myd is a soft link of mine to /home/foouser/my-server/xspyctra/xspyctrad.py, I

  1. changed ExecStart = /home/foouser/my-server/xspyctra/xspyctrad.py
  2. commented WorkingDirectory in which I'm not actually interested, since the python script will do a os.chdir('/home/foouser/my-server/bin') by its own

Services reloaded, system rebooted, etc. But now I'm stunned from the following error

systemd[1653]: myd.service: Failed at step EXEC spawning /home/foouser/my-server/xspyctra/xspyctrad.py: No such file or directory

systemd[1]: myd.service: Main process exited, code=exited, status=203/EXEC

I can grant for sure that that file exists

$ ls -l /home/foouser/my-server/xspyctra/xspyctrad.py
-rwxrw-r-- 1 foouser foouser 5532 May 10 16:13 /home/foouser/my-server/xspyctra/xspyctrad.py

As a last diagnostic, service myd status

● myd.service - My Server Daemon
  Loaded: loaded (/etc/systemd/system/myd.service; enabled; vendor preset: enabled)
  Active: failed (Result: exit-code) since Fri 2018-05-11 10:58:21 CEST; 16min ago
  Docs: https://etcetc.org
  Main PID: 1627 (code=exited, status=203/EXEC)

EDIT 1

However, this could somehow happens only during boot time. As a matter of fact,

sudo service myd stop
sudo service myd start

seems working, because service myd status

● myd.service - My Server Daemon
     Loaded: loaded (/etc/systemd/system/myd.service; enabled; vendor preset: enabled)
     Active: activating (start) since Fri 2018-05-11 11:22:52 CEST; 1min 16s ago
       Docs: https://etcetc.org
   Main PID: 2106 (python2)
      Tasks: 24
     Memory: 545.1M
        CPU: 10.913s
     CGroup: /system.slice/myd.service
       ├─2106 python2 /home/foouser/my-server/bin/myd
       ├─2122 ./<my-child-process-a>
       ├─2127 ./<my-child-process-b>
       └─2133 ./<my-child-process-c>

As expected.
Furthermore, notice that xspyctrad.py, the actual script, will spawn a twisted.reactor, hence will not return and stay in foreground.

So where could be my mistake ?

Best Answer

Besides moving executables to an early-accessible location (which is anyway a good thing to do), it is possible specify mount dependencies to tell systemd to delay start-up of your unit until after the location is mounted and available.

You can either use RequiresMountsFor=:

[Unit]
RequiresMountsFor=/home/foouser/my-server/bin

Or specify Requires=/After= on a corresponding .mount unit, if you know exactly what needs to be mounted:

[Unit]
Requires=home-foouser.mount
After=home-foouser.mount
Related Question