Aphire Aphire - 1 year ago 83
Linux Question

Access file system from Python on boot-up cron

Please consider the following problem:

I have a Python script that runs on a linux machine (Raspberry pi 3, running Rasbian Jessie) on boot.
This script has been added to

sudo crontab -e

The script itself starts with no problem but is unable to load in a certain file that is in the same directory as the script (Desktop), I have any print statements/issues going into a log file. Which reads as follows:

Traceback (most recent call last):
File "/home/pi/Desktop/mainServ.py", line 18, in <module>
mouth_detector = dlib.simple_object_detector(mouth_detector_path)
RuntimeError: Unable to open mouthDetector.svm

I assume this is because the script has no access to a filesystem, or perhaps LXDE/Desktop at boot time? I could very well be wrong on this.

Any solutions to this problem will be greatly appreciated.

Answer Source

Whenever you execute a script via crontab be ready for environment variables to be different. In this case you can simply use the whole path in the file you are trying to reference.

To see what the current environment variables are from within Python, use:

    import os

You may find there are other differences between the crontab environment and whatever interpreter environment you are using for testing.