Sample Header Ad - 728x90

Running a systemd service with RootDirectory= and access /bin binaries

2 votes
2 answers
8294 views
I'm trying to utilize some of the systemd helpers to [chroot(2)](http://man7.org/linux/man-pages/man2/chroot.2.html) the process using RootDirectory=. It's a rudimentary Python script that lives under /srv/http that hosts a web server. It has a shebang #!/usr/bin/python *(and I've also tried different combinations)* The service file is also quite simple:
[Unit]
Wants=network-online.target
After=network-online.target

AssertPathExists=/srv/http

[Service]
Type=simple
Restart=always
RestartSec=10

RootDirectory=/srv/http
PrivateTmp=true

ExecStart=/server.py
PIDFile=/run/miniweb.pid

[Install]
WantedBy=multi-user.target
The log clearly says it can't find the executable:
Mar 11 19:23:49 bigrigv2 systemd: testweb.service: Failed to execute /server.py: No such file or directory
Mar 11 19:23:49 bigrigv2 systemd: testweb.service: Failed at step EXEC spawning /server.py: No such file or directory
It's marked as executable:
-rwxr-xr-x 1 anton anton  650 Mar 11 19:06 server.py
I also tried ExecStart=/bin/python /srv/http/server.py and other variations. I'm not entirely sure I even understand the concept of RootDirectory and how to properly execute for instance Python or other binaries from a chrooted service script. My assumption is that before executing the service, it chroot:s in to /srv/http after which the service won't be able to back out and execute Python in this case. Which would make sense, but then I don't quite get why /server.py isn't found. And how would you execute things that are dependent on other binaries? Most solutions mention utilizing the language (C for instance) chroot and control it from the application, but then I don't understand the point of offering chroot in the service script for other things than very limited bash scripts or standalone binaries. Probably an extremely easy problem, but I'm quite lost and any help would be appreciated!
Asked by Torxed (3727 rep)
Mar 11, 2021, 07:13 PM
Last activity: Jun 17, 2025, 12:48 PM