2

I made a file /root/startup.sh (I am on Ubuntu with Gnome 3.22.3 with root access) with the following contents:

#!/bin/bash
DISPLAY=":0"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
SHELL="/bin/bash"

$(/usr/bin/xinput list > /root/Desktop/output.txt)

and I have put this line in the crontab -e file:

* * * * *  /bin/bash /root/startup.sh

but after a minute when the file output.txt is created and when I open it it is empty and there is no contents in it.
Why?

  • Are you putting it in you user's crontab, or root's? My guess is it's unable to connect to the X server. First I suggest removing the command substitution $( . . . ) and redirecting errors to the file as well /usr/bin/xinput list > /root/Desktop/output.txt 2>&1 – steeldriver May 4 '17 at 10:26
  • @steeldriver checkout the answer. – newpeople May 4 '17 at 18:49
1

There are so many articles on web, so many questions and answers about this issue but non of them have notified me of this silly mistake:

export DISPLAY=":0"
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export SHELL="/bin/bash"

This should be used instead the 3 lines in my question.
I should have added export before them.
And it works.

1

In my experience you also need to set the XAUTHORITY variable.

export XAUTHORITY=/home/<user>/.Xauthority

As you are starting your script as root via cron, likely the root user does not have a .Xauthority file. But because you ARE root in the cron session, you can use the one from your regular user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.