run command on user display as root isn't working as expected

8,809

I found the answer on https://www.reddit.com/r/linux/comments/2clob0/help_with_cron_though_its_probably_not_really_a/?st=j7e4jv37&sh=e83bf930

I had to add the following to the script:

export DISPLAY=:0
export XAUTHORITY=/home/username/.Xauthority
Share:
8,809
stdcerr
Author by

stdcerr

Coding mostly on embedded systems (24/7/365 uptime) to make a living at daytime, coding on a variety of systems (x86, arm) for fun at nighttime, preferences: Linux, C & C++. Always interested in learning other ways/better ways to solve problems!

Updated on September 18, 2022

Comments

  • stdcerr
    stdcerr almost 2 years

    I'm trying to execute a xdotool operation (move mouse) on the user's display from a root shell but I get:

    # DISPLAY=:0 mousemove_relative 1 1
    No protocol specified
    Error:Can't open display: (null)
    Failed creating new xdo instance
    #
    

    Why does this not work?


    the script:

    #!/bin/bash
    
    exec=`/usr/sbin/nethogs -t -c 1| awk '/smbd/ {print $2}'`
    
    sent=$exec
    
    echo "result: "$sent
    sent=$(echo "($sent+0.5)/1" | bc)
    echo "rounded: "$sent
    if [[ $sent -gt 0 ]]
    then 
        echo "music is playing"
        `DISPLAY=:0 xdotool mousemove_relative 1 1`
    else
        echo "quiet"
    fi
    

    I get this in /var/mail/root:

    Waiting for first packet to arrive (see sourceforge.net bug 1019381)
    result: 6.73438
    rounded: 7
    music is playing
    No protocol specified
    Error: Can't open display: (null)
    Failed creating new xdo instance
    
    • Admin
      Admin almost 7 years
      I suggest to use user which owns this X11 session.
    • Admin
      Admin almost 7 years
      Yes, but I have to run this script as root for reasons that will be out of scope of the question asked...
    • Admin
      Admin almost 7 years
      Is mousemove_relative a shell script or function? If so, can you show the contents to us?
    • Admin
      Admin almost 7 years
      @MarkPlotnick it's a script, please see above, it'll run as a cront job from root's table