M BUZZ CRAZE NEWS
// general

Create a Systemd service in Ubuntu to run MKDocs

By Emma Johnson

I am running a server with MKDocs for documentation.

In order to use the MKDocs from the directory where the project is you need to run mkdocs serve -a 192.168.3.107:8080 in order to get it to start.

So far I was using /usr/bin/tmux new-session -d -s "MKDOCS" "cd /root/mkdocs && mkdocs serve -a 192.168.3.107:8080" to have it run in the background but this is a very sketchy solution and I would like to run it as systemd Service.

I was trying something like this but it doesn't work:

[Unit]
Description=mkdocs service
ConditionPathExists=/root/mkdocs
[Service]
Type=simple
User=root
WorkingDirectory=/usr/local/bin
ExecStart=/root/mkdocs serve -a 192.168.3.107:8080
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

After I run the service with start, the status shows :

root@server:~/mkdocs# service mkdocs status
● mkdocs.service - mkdocs service Loaded: loaded (/etc/systemd/system/mkdocs.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2022-02-02 13:54:44 CET; 3min 46s ago Process: 379585 ExecStart=/root/mkdocs serve -a 192.168.3.107:8080 (code=exited, status=203/EXEC) Main PID: 379585 (code=exited, status=203/EXEC)

I get journalctl -xe shows:

-- The job identifier is 368094.
Feb 02 13:54:44 server.domain.local systemd[379585]: mkdocs.service: Failed to execute command: Permission denied
Feb 02 13:54:44 server.domain.local systemd[379585]: mkdocs.service: Failed at step EXEC spawning /root/mkdocs: Permission denied
-- Subject: Process /root/mkdocs could not be executed
-- Defined-By: systemd
-- Support:
--
-- The process /root/mkdocs could not be executed and failed.
--
-- The error number returned by this process is ERRNO.
Feb 02 13:54:44 server.domain.local systemd[1]: mkdocs.service: Main process exited, code=exited, status=203/EXEC
-- Subject: Unit process exited
-- Defined-By: systemd
-- Support:
--
-- An ExecStart= process belonging to unit mkdocs.service has exited.
--
-- The process' exit code is 'exited' and its exit status is 203.
Feb 02 13:54:44 server.domain.local systemd[1]: mkdocs.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support:
--
-- The unit mkdocs.service has entered the 'failed' state with result 'exit-code'.

I have given the directory full permissions with chmod -R 777 /root/mkdocs however I still get the same error.

Hopefully, someone can help me get it working. Thank you

EDIT:

I have Changed the path, still will 777 permissions, so the code now is:

however it still doesn't work, I am getting :

root@server:/test# systemctl daemon-reload
root@server:/test# systemctl start mkdocs.service
root@server:/test# systemctl status mkdocs.service
● mkdocs.service - mkdocs service Loaded: loaded (/etc/systemd/system/mkdocs.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2022-02-02 14:17:24 CET; 4s ago Process: 380128 ExecStart=/test mkdocs serve -a 192.168.3.107:8080 (code=exited, status=203/EXEC) Main PID: 380128 (code=exited, status=203/EXEC)
Feb 02 14:17:24 server.domain.local systemd[1]: Started mkdocs service.
Feb 02 14:17:24 server.domain.local systemd[380128]: mkdocs.service: Failed to execute command: Permission denied
Feb 02 14:17:24 server.domain.local systemd[380128]: mkdocs.service: Failed at step EXEC spawning /test: Permission denied
Feb 02 14:17:24 server.domain.local systemd[1]: mkdocs.service: Main process exited, code=exited, status=203/EXEC
Feb 02 14:17:24 server.domain.local systemd[1]: mkdocs.service: Failed with result 'exit-code'.

and journalctl -xe shows:

-- The unit mkdocs.service has entered the 'failed' state with result 'exit-code'.
Feb 02 14:20:32 server.domain.local systemd[1]: Started mkdocs service.
-- Subject: A start job for unit mkdocs.service has finished successfully
-- Defined-By: systemd
-- Support:
--
-- A start job for unit mkdocs.service has finished successfully.
--
-- The job identifier is 368972.
Feb 02 14:20:32 server.domain.local systemd[380164]: mkdocs.service: Failed to execute command: Permission denied
Feb 02 14:20:32 server.domain.local systemd[380164]: mkdocs.service: Failed at step EXEC spawning /test: Permission denied
-- Subject: Process /test could not be executed
-- Defined-By: systemd
-- Support:
--
-- The process /test could not be executed and failed.
--
-- The error number returned by this process is ERRNO.
Feb 02 14:20:32 server.domainlocal systemd[1]: mkdocs.service: Main process exited, code=exited, status=203/EXEC
-- Subject: Unit process exited
-- Defined-By: systemd
-- Support:
--
-- An ExecStart= process belonging to unit mkdocs.service has exited.
--
-- The process' exit code is 'exited' and its exit status is 203.
Feb 02 14:20:32 server.domain.local systemd[1]: mkdocs.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support:
--
-- The unit mkdocs.service has entered the 'failed' state with result 'exit-code'.

it doesn't make any sense since the permissions are 777.

5

1 Answer

I managed to fix it by creating script: /root/mkdocs/run.sh

#!/bin/bash
cd /root/mkdocs
mkdocs serve -a 192.168.3.107:8080

And changing the service to:

[Unit]
Description=mkdocs service
ConditionPathExists=/root/mkdocs
[Service]
Type=simple
User=root
WorkingDirectory=/usr/local/bin
ExecStart=/usr/bin/env bash /root/mkdocs/run.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

I'm leaving this answer in case someone looks for it in the future. file needs to be places in /etc/systemd/system/mkdocs.service

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy