Cron Job: How to Run a Script That Requires to Open Display

Cron job: how to run a script that requires to open display?

You will need a valid DISPLAY and XAUTHORITY to use X-Programms in Cron!

To set DISPLAY is very easy, type in the Bash:

export DISPLAY=":0.0"

In order to get a valid XAUTHORITY you have to look for it.
Under Debian/Gnome/gdm3 they are saved in var/run/gdm3/*/database
I used the following script:

export DISPLAY=":0.0"
[ -z $USER ] && USER=$( who | awk '{ print $1 }' | sort | uniq >/tmp/test )
for I in /var/run/gdm3/*; do
AUTHUSER="`echo $I | awk -F '-' '{ print $3 }'`"
for J in $USER; do
[ "${AUTHUSER}" = "${J}" ] || continue
USER="$J"
export XAUTHORITY="${I}/database" && break
done
done
sudo -u ${USER} /Path/to/xProgramm

The Var $USER can be empty, than the script looks for a valid user, otherwise you can tell the script the var!

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

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

How can I run a cron job that depends on a desktop without a permanent remote desktop connection open?

If you don't have a display attached, you'll need to find a headless system that can run with a virtual DISPLAY. Headless Chrome is probably the easiest for you to swap in, and has convenient python bindings [related question].

You might also want to look into running selenium, depending on the task you need to run.

Finally, you might be interested in running a VNC server; depending on the config of your server, you can configure it to stay alive while your client disconnects. usually this is useful for temporary network hiccups, but set to a high enough timeout value, you may get the effects of a persistent, virtual, non-transient display.

how to set cron to display gui application

You can do this by setting the DISPLAY variable to :0. For instance:

* * * * * export DISPLAY=:0; gedit

This crontab line will open the gui software gedit every minute.



Related Topics



Leave a reply



Submit