bashcronenv

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


I'm currently working on a multimedia machine, which I want to control via a PS4 DS4 (using ds4drv and a bit of scripting). Everything works like a charm if started via Bash / Terminal manually. But almost nothing works if started via crontab.

So far I have done some research and it seem like the error is somehow related to my environment vars.

Also ds4drv keeps crashing if it is started with the vars of cron:

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'

The crontab:

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

The log show only the above output if at all.

I have already tried to specify cron's environment

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

but without any luck.

What am I doing wrong?


Solution

  • You have a semicolon after invoking bash which is incorrect. Try creating a script with the following content (let's say /home/hehxes/run_ds4drv.sh):

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

    Then add execute file permissions:

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

    And call the script in cron:

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