0

On my Beaglebone Black device, I want to run python code at startup which is shared here https://github.com/acseckin/hmrid. The python code requires super user privileges. The Debian version I used on the device was installed with the image "Debian 8.7 2017-03-19 4GB SD IOT". The code works fine from the terminal.

sudo python /home/debian/hmrid/runhmrid.py

Debian users and roots crontab does not work when I append the following line.

@reboot sudo python /home/debian/hmrid/runhmrid.py

But other code that does not require a super user is working perfectly when I add a crontab for a Debian user like

@reboot python /home/debian/hmrid/runNotSuperUser.py

15
  • 1
    Why not put the code to be run as root into the crontab file for root ?
    – thrig
    Jun 28, 2017 at 18:45
  • 1
    Like @thrig said: Replace sudo with root.
    – jc__
    Jun 28, 2017 at 18:55
  • 1
    Yes, I am also agree with thrig. Instead of using sudo you can set cron entry for the user root
    – Sourav
    Jun 28, 2017 at 19:07
  • 1
    Where is the python executable? Perhaps use the fully qualified path as in @reboot sudo /path/to/python /home/debian/hmrid/runhmrid.py?
    – Deathgrip
    Jun 28, 2017 at 20:57
  • 2
    Change the redirect to >/tmp/runhmrid.out 2>&1
    – Deathgrip
    Jun 28, 2017 at 22:36

1 Answer 1

2

Place the job in root's crontab with sudo crontab -e as

@reboot /full/path/to/python /home/debian/hmrid/runhmrid.py

Be aware that the job will be executed without your usual environment. This means that environment variables that may affect the way Python behaves may have to be set elsewhere for the script to work, if it relies on them somehow.

If you want to log the output from this command to a separate file, you may use

@reboot /full/path/to/python /home/debian/hmrid/runhmrid.py >/tmp/runhmrid.log 2>&1

This will log any output from the cron job to the file /tmp/runhmrid.log including error messages.

You may also create a shell script wrapper that sets up the environment (using a series of export statements) and starts you Python script. Then you may call that script from cron.

3
  • not worked for me. python is working. I think root crontab can not start.
    – acs
    Jun 28, 2017 at 21:45
  • @acs "Doesn't work" is not easy to debug. See update about logging and try that. Than let us know what went wrong.
    – Kusalananda
    Jul 1, 2017 at 11:36
  • 1
    Thank to @Deathgrip and you for helping me find my mistake. I understand that my problem is not actually about crontab. It does not work because of the inaccuracies in the code library. here is solution github.com/adafruit/adafruit-beaglebone-io-python/issues/…
    – acs
    Jul 1, 2017 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.