I have a simple bash script:

while [[ true ]]
    status="$(xset q | grep 'Monitor is Off')"
    if [[ $status != '' ]] ; then
        #shutdown the computer
        dbus-send --system --print-reply --dest=org.freedesktop.login1.Manager.Poweroff" boolean:true

So whenever my screen get off my computer shutdown. This script work well, when i launch it from terminal, but I want to execute it every boot. I placed a symbolic link in rc5.d and seems to work. pgrep poweroff show script PID but, guess! if the screen get off pc don't shutdown. I don't undestand why.

  • Xset is a user preferences utility. Add it to startup applications (Dash > Startup Applications) and it will run on login as your user. It will work then. – Jacob Vlijm Apr 22 '17 at 18:43


Your script is started during boot, thus not following the login process, therefor it has no environment variables or perhaps a very limited set. I trust there is no DISPLAY, perhaps not even a PATH. So the xset command fails, status stays empty, the dbus-send never reached.
You can verify my explanation by changing the xset command in your script to:

status="$(xset q 2>/tmp/output.err | grep 'Monitor is Off')"

then reboot and check the file /tmp/output.err


I don't have the exact solution for you. You will have to do some work and various reboots and error checks.
Oh.. I urge you to change the name of your script. You named it poweroff and so did I, forgetting it is a system command. So guess what happend when I ran my script...

Ok, first action.
In the top of your script add:

export DISPLAY=:0.0
env > /tmp/output.log

Within the while-loop add

sleep 5

command to prevent a huge /tmp/output.err file.

Reboot your system and check the output files in /tmp. If there is an empty output.err, then the xset command did not fail. If it did fail, you need to define more environment variables in your script. Perhaps one or more of the XDG_ variables, sorry but I'm not very experienced with X11.
Good luck !

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.