Unable to init server: Could not connect: Connection refused gxmessage: unable to initialize GTK

This works.

#!/bin/bash
#
# located in /lib/systemd/system-sleep/
# Created 4/2/19
exec 2> /tmp/systemd_suspend_test_err.txt
if [ "${1}" = "pre" ]; then
# Do the thing you want before suspend here
echo "we are suspending." > /tmp/systemd_suspend_test.txt
elif [ "${1}" = "post" ]; then
# Do the thing you want after resume here
echo "and we are back from being suspended" >> /tmp/systemd_suspend_test.txt
sleep 10
#notify-send "Computer is returning from suspend." -t 2000
# gxmessage does NOT work
/usr/bin/gxmessage -fg red -font 'sans 30' -timeout 3 'Back from being suspended.'
fi

But for some reason, gxmessage will not work.

I get this.

Unable to init server: Could not connect: Connection refused
gxmessage: unable to initialize GTK

Is there a way to manually initialize GTK?

Before suspending, you can write xhost + which will allow clients to connect from any host (not recommended in production env - security concerns). Then, suspend and resume, the box with the red text appears. Please add, gxmessage -fg red -display :0 'blahblah' (tested on my laptop)

1 Like

Thanks for the help.