Env & Crontab -> 程序在 bash 中运行但不会通过 cron 启动

Env & Crontab -> Program works in bash but won't start via cron

我目前正在使用一台多媒体机器,我想通过 PS4 DS4(使用 ds4drv 和一些脚本)来控制它。如果通过 Bash / Terminal 手动启动,一切都像魅力一样。但是如果通过 crontab.

启动几乎没有任何效果

到目前为止,我已经做了一些研究,似乎错误与我的环境变量有某种关系。

如果使用 cron 的变量启动,ds4drv 也会不断崩溃:

hehxes@PC:~$ env - `cat ~/cronenv` /bin/sh
$ /usr/local/bin/ds4drv
[info][controller 1] Created devices /dev/input/js2 (joystick) /dev/input/event25 (evdev) 
[info][controller 1] Connected to Bluetooth Controller (1C:66:6D:64:2F:74 hidraw5)
[info][hidraw] Scanning for devices
[info][controller 1] Battery: 75%
[info][controller 1] Switching to profile: kbmouse
[info][controller 1] Switching to profile: gaming
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 110, in run
    self.loop.run()
  File "build/bdist.linux-x86_64/egg/ds4drv/eventloop.py", line 105, in run
    callback()
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 107, in read_report
    self.fire_event("device-report", report)
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 39, in fire_event
    self.loop.fire_event(event, *args)
  File "build/bdist.linux-x86_64/egg/ds4drv/eventloop.py", line 90, in fire_event
    self.process_events()
  File "build/bdist.linux-x86_64/egg/ds4drv/eventloop.py", line 96, in process_events
    callback(*args)
  File "build/bdist.linux-x86_64/egg/ds4drv/action.py", line 73, in _handle_report
    self.handle_report(report)
  File "build/bdist.linux-x86_64/egg/ds4drv/actions/binding.py", line 105, in handle_report
    binding.callback(report, *binding.args)
  File "build/bdist.linux-x86_64/egg/ds4drv/actions/binding.py", line 62, in <lambda>
    lambda r: self.controller.next_profile())
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 62, in next_profile
    self.load_profile(self.profiles[next_index])
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 48, in load_profile
    self.load_options(profile_options)
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 94, in load_options
    self.fire_event("load-options", options)
  File "build/bdist.linux-x86_64/egg/ds4drv/__main__.py", line 39, in fire_event
    self.loop.fire_event(event, *args)
  File "build/bdist.linux-x86_64/egg/ds4drv/eventloop.py", line 90, in fire_event
    self.process_events()
  File "build/bdist.linux-x86_64/egg/ds4drv/eventloop.py", line 96, in process_events
    callback(*args)
  File "build/bdist.linux-x86_64/egg/ds4drv/actions/input.py", line 75, in load_options
    self.joystick.device.close()
  File "/usr/lib/python2.7/dist-packages/evdev/uinput.py", line 115, in close
    self.device.close()
AttributeError: 'NoneType' object has no attribute 'close'

crontab:

@reboot /usr/local/bin/ds4drv > /home/hehxes/ds4drv/verbose.log

如果有的话,日志只显示上面的输出。

我已经尝试指定 cron 的环境

@reboot /bin/bash; . /home/hehxes/.profile; /usr/bin/screen -dmS ds4drv-screen /usr/local/bin/ds4drv

但运气不好。

我做错了什么?

你在调用 bash 后有一个分号,这是不正确的。尝试创建一个包含以下内容的脚本(假设 /home/hehxes/run_ds4drv.sh):

#!/usr/bin/env bash
. /home/hehxes/.profile
/usr/local/bin/ds4drv

然后添加执行文件权限:

chmod a+x /home/hehxes/run_ds4drv.sh

并在 cron 中调用脚本:

@reboot /home/hehxes/run_ds4drv.sh > /home/hehxes/ds4drv/verbose.log