启动时运行 python3 脚本时出错

启动时运行 python3 脚本时出错

我正在尝试通过制作运行 python 脚本的 bash 脚本( /etc/init.d/file.bash )在启动时运行 python 脚本( /home/user/Desktop/file.py )

#!/bin/bash

case "$1" in
  start)
    echo "Starting"
    /home/user/Desktop/file.py
    ;;
  stop)
    echo "Stopping"
    killall python3.5
    ;;
  *)
    echo "Usage: service file.bash{start|stop}"
    exit 1
    ;;
esac
exit 0

我创建了 file.bash 的符号链接并将其放入 /etc/rc5.d/S{NN}file.bash 但由于某种原因,当服务启动时我收到错误

● file.service
   Loaded: loaded (/etc/init.d/file.bash; bad; vendor preset: enabled)
   Active: active (exited) since Mon 2017-12-11 14:49:45 EET; 29min ago
     Docs: man:systemd-sysv-generator(8)
   Process: 1285 ExecStart=/etc/init.d/file.bash start (code=exited, status=0/SUCCESS)

Dec 11 14:49:45 ****-PC file.bash[1285]:     protocol_display.Display.__init__(self, *args, **keys)
Dec 11 14:49:45 ****-PC file.bash[1285]:   File "/usr/local/lib/python3.5/dist-packages/Xlib/protocol/display.py", line 85, in __init__
Dec 11 14:49:45 ****-PC file.bash[1285]:     name, host, displayno, screenno = connect.get_display(display)
Dec 11 14:49:45 ****-PC file.bash[1285]:   File "/usr/local/lib/python3.5/dist-packages/Xlib/support/connect.py", line 72, in get_display
Dec 11 14:49:45 ****-PC file.bash[1285]:     return mod.get_display(display)
Dec 11 14:49:45 ****-PC file.bash[1285]:   File "/usr/local/lib/python3.5/dist-packages/Xlib/support/unix_connect.py", line 61, in get_display
Dec 11 14:49:45 ****-PC file.bash[1285]:     raise error.DisplayNameError(display)
Dec 11 14:49:45 ****-PC file.bash[1285]: Xlib.error.DisplayNameError: Bad display name ""
Dec 11 14:49:45 ****-PC systemd[1]: Started file.service.

当我启动 file.bash( $ /etc/init.d/file.bash start) 时,没有收到任何错误

我正在使用 Ubuntu 16.04.1 和 python3.5

编辑:

错误在于(from pynput.keyboard import Key, Listener

#!/usr/bin/python3.5
import socket
import subprocess
import time
import os
from pynput.keyboard import Key, Listener # <

但当我在终端中调用该脚本时,它正常工作

相关内容