Search code examples
unixdebiancronxserverxauth

Start X Program with /etc/crontab on Debian 6.0.4


I am trying to start a X-Programm (e.g. gedit or Firefox) with crontab. I have searched quite a while, and the forums are full of help, unfortunately nothing was usefull for me.

What I am trying is, to start a X-Programm by Crontab. For testing Purposes I have done the following:

Entry in my "/etc/crontab" (my User-Account is mrs, I am logged on):

* * * * * mrs /bin/bash /tmp/test.sh

cat /tmp/test.sh:

#!/bin/bash
DISPLAY=":0.0"
gedit 1> /tmp/out 2>/tmp/err

cat /tmp/err:

No protocol specified
(gedit:3456): Gtk-WARNING **: cannot open display: :0.0

/tmp/out is empty!


Solution

  • I have found the solution! The Problem is, that I did not had an XAUTHORITY. So I had to get a valid XAUTHENTICATION. I have tried a lot with xhost and xauth, but it did not help. Now I found another simple way wich works with gnome/gdm3, if you have access to the root Crontab:

    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} /Pfad/zum/xProgramm
    

    Die Variable $USER kann man vorher belegen, andernfalls wird sie mit mindestens einem bereits eingeloggten User beschrieben. Vorraussetzung ist allerdings, dass ein User am System auch angemeldet ist.

    Ich habe dies getestet, und es klappt problemlos!