Search code examples
c++qtx11xauth

Qt: launch a message box periodically with crontab in ubuntu


I am trying to launch a Qt message box every minute. I added a line in crontab to run the Qt program's executable and redirected the error output to a file in my home directory. There is no problem with the Qt program that launches the message box because I tested it, but when I try to launch it with crontab the following error arises:

QXcbConnection: Could not connect to display
Aborted (core dumped)

I checked that over the internet and found a thread that might be helpful: https://unix.stackexchange.com/questions/148945/could-not-connect-to-display-in-one-user-account/149026#149026
I believe the first response gives the solution but it's not clear.
It suggests to use x11 to share the desktop and xauth to add security measures but doesn't specify how to configure them. I have xauth already installed but the error persists. Any ideas about how to solve this problem?


Solution

  • I think the DISPLAY environment is missing. I suggest you wrap your application in a wrapper script (let's call it horloge.sh):

    #!/bin/sh
    DISPLAY=:0
    export DISPLAY
    /home/salwa/computing/cpp/horloge
    

    Then put the horloge.sh in your crontab. Don't forget to do a chmod u+x horloge.sh so that the script is executable.