在 Ubuntu 中创建 Systemd 服务以运行 MKDocs

在 Ubuntu 中创建 Systemd 服务以运行 MKDocs

我正在运行一个带有 MKDocs 的服务器来进行文档记录。

为了使用项目所在目录中的 MKDocs,您需要运行mkdocs serve -a 192.168.3.107:8080它以启动它。

到目前为止,我一直/usr/bin/tmux new-session -d -s "MKDOCS" "cd /root/mkdocs && mkdocs serve -a 192.168.3.107:8080"让它在后台运行,但这是一个非常粗略的解决方案,我想将它作为 systemd 服务运行。

我尝试过这样的事情,但它不起作用:

[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

使用启动运行服务后,状态显示:

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)

我得到 journalctl -xe 显示:

-- 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'.

我已授予该目录完全权限chmod -R 777 /root/mkdocs但仍然出现相同的错误。

希望有人能帮助我实现它。谢谢

编辑:

我已经改变了路径,仍然有 777 权限,所以现在的代码是:

但它仍然不起作用,我得到:

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'.

journalctl -xe显示:

-- 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'.

由于权限是 777,所以这没有任何意义。

答案1

我设法通过创建脚本来修复它:/root/mkdocs/run.sh

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

并将服务更改为:

[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

我留下这个答案以防将来有人寻找它。文件需要放在/etc/systemd/system/mkdocs.service

相关内容