Ubuntu: Why my script doesn't work on boot?


I have a simple bash script:

while [[ true ]]  do      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      fi  done  

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.



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 !

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »