Homunculus Reticulli
Homunculus Reticulli

Reputation: 68466

Cron job can't run bash script (that shells python script requiring xsession) that runs perfectly well from console

I am using dryscrape in a python script. The python script is called in a bash script, which is run by cron. For those who may not be aware, dryscrape is a headless browser (use QtWebkit in the background - so requires an xsession).

Here are the main points concerning the issue I'm having

  1. When I run the python script from the command line, it works
  2. When I run the bash script from the command line, it works too

I figured out that this may have something to do with different environments between my command prompt and when the cron job is running, so I modified my bash script to source my .profile as follows:

#/bin/bash

. /full/path/to/my/home/directory/.profile

python script_to_run.py

This is what my cronjob crontab entry looks like:

0,55 14-22 * * 1-5 /path/to/script.sh >> $(date "+/path/to/logs/\%Y\%m\%d.mydownload.log" )

By the way, I know that the job is being run (I can see entries in /var/log/syslog, and the script also writes to a log file - which is where I get the error message below):

In all cases, I got the following error message:

Could not connect to X server. Try calling dryscrape.start_xvfb() before creating a session

I have installed the prerequisites, on my machine (obviously - since it runs at the command line). At the moment, I have run out of ideas.

What is causing the script to run fine at the console, and then fail when run by cron?

[[Relevant Details]]

Upvotes: 1

Views: 202

Answers (2)

Homunculus Reticulli
Homunculus Reticulli

Reputation: 68466

The solution to this was to call the dryscrape.start_xvfb() method before starting the dryscrape session.

Upvotes: 1

AwkMan
AwkMan

Reputation: 670

Cron user does not have display, so you cannot run any command which requires a display. You need to modify the python script to do not use any type of display (check carefully, because some python commands, even though they do not open any display , they internally check for this variable). The best way to test is to ssh into the machine without Display, and check if you can run it from there without erros.

Upvotes: 0

Related Questions