Score:0

Create a Systemd service in Ubuntu to run MKDocs

ye flag

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: http://www.ubuntu.com/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: http://www.ubuntu.com/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: http://www.ubuntu.com/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: http://www.ubuntu.com/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: http://www.ubuntu.com/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: http://www.ubuntu.com/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: http://www.ubuntu.com/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.

uz flag
Jos
Why doesn't it work? You seem to be on the right track. What happens with `sudo systemctl start mkdocs` or `sudo systemctl status mkdocs` ?
ye flag
@Jos I edited the original question with more information, thanks for taking a look
uz flag
Jos
It's a permissions problem. The executable has the wrong permissions, or you didn't start the service with `sudo`. In any case, `/root` is not a very good location for server executables.
ye flag
@Jos I updated the post with EDIT, I changed the path and gave 777 permissions, but still having an issue. hopefully you can take a look. Thanks. I belive the issue is the way I wrote the command. I am supposed to be In the directory where docs are, and run mkdocs, which is found : whereis mkdocs mkdocs: /usr/local/bin/mkdocs /usr/share/man/man1/mkdocs.1.gz
ye flag
@guiverc I updated the tags, I am actually testing on 20.04
Score:0
ye flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.