I am trying to automate QGIS (2.18.4) standalone scripts in ubuntu using crontab. My scripts execute well when running from command, but fail if I put in crontab.
from qgis.core import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.gui import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
In crontab, it fails at app = QApplication([])
. Crontab gives error as
: cannot connect to X server
How to make my QGIS standalone script run in crontab?
1 Answer 1
This problem is actually not related to this community, you should ask in unix.stackexchange.com to get better answers.
To run a GUI command with a cronjob, you'll have to tell cron what display the program should use:
export DISPLAY=:0
:0
is the default Display. If you like the program to run on other display, please change the number accordingly (e.g. :1, :2, etc).
Your cron command will look like this:
* * * * * export DISPLAY=:0 && python /path/to/your/command.py