帮助解决自定义服务问题

帮助解决自定义服务问题

我想设置运行 Ubuntu Server 的机器,使其abcde在插入光盘时自动运行。

  • /usr/local/bin/discready当光盘等待读取时返回 0,否则返回非零
  • /usr/local/bin/autorip运行discready,每 15 秒检查一次 0 退出代码,并abcde在满足该条件时运行。
  • /etc/systemd/system/autorip.service使用以下指令创建本文

/usr/local/bin/discready脚本:

#!/usr/bin/python3
# usage: DEVICE=/dev/sr0 discready

import fcntl
import os
import sys

DEFAULT_DEVICE = '/dev/cdrom'
STATUSES = ['NA', 'NO_DISK', 'OPEN', 'READING', 'DISC']

def drive_status(device):
    # https://superuser.com/a/1367091/1001393
    file = os.open(device, os.O_RDONLY | os.O_NONBLOCK)
    status = fcntl.ioctl(file, 0x5326)
    os.close(file)
    return status

if __name__ == '__main__':
    device = os.environ.get('DEVICE', DEFAULT_DEVICE)
    status_num = drive_status(device)
    status = STATUSES[status_num]

    if status_num == STATUSES.index('DISC'):
        sys.exit(0)

    print(f'Device: {device}, Status: {status}')
    sys.exit(status_num)

/usr/local/bin/autorip脚本:

#!/usr/bin/env bash
# wait for discready to return a zero exit code, run abcde

while true; do
  discready
  result=$?
  [ $result -eq 0 ] && abcde -N
  sleep 15
done

/etc/systemd/system/autorip.service

Description=autorip
After=network.target

[Service]
Type=simple
User=rd
WorkingDirectory=/home/rd
ExecStart=/home/rd/autorip
Restart=always

[Install]
WantedBy=multi-user.target

输出systemctl status autorip.service

● autorip.service - autorip
   Loaded: loaded (/etc/systemd/system/autorip.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2019-02-24 05:22:36 UTC; 18min ago
  Process: 20642 ExecStart=/home/rd/autorip (code=exited, status=203/EXEC)
 Main PID: 20642 (code=exited, status=203/EXEC)

Feb 24 05:22:36 kingwin systemd[1]: autorip.service: Main process exited, code=exited, status=203/EXEC
Feb 24 05:22:36 kingwin systemd[1]: autorip.service: Failed with result 'exit-code'.
Feb 24 05:22:36 kingwin systemd[1]: autorip.service: Service RestartSec=100ms expired, scheduling restart.
Feb 24 05:22:36 kingwin systemd[1]: autorip.service: Scheduled restart job, restart counter is at 5.
Feb 24 05:22:36 kingwin systemd[1]: Stopped autorip.
Feb 24 05:22:36 kingwin systemd[1]: autorip.service: Start request repeated too quickly.
Feb 24 05:22:36 kingwin systemd[1]: autorip.service: Failed with result 'exit-code'.
Feb 24 05:22:36 kingwin systemd[1]: Failed to start autorip.

autorip在终端中运行时,它完全按照我的要求工作。尝试将其转变为服务时,我遇到了麻烦。是因为 discready 返回了非零退出代码吗?任何故障排除提示或指示都将不胜感激。

答案1

我的服务路径错误。

相关内容