3

Since I did not receive any answer to this question and I have to manually restart gnome-shell each time the PC comes back from suspend to correct the distorted colors, I would like to know how to automatically restart Gnome Shell after suspend instead of doing Alt + F2 - r.

Installed Gnome extensions: Dash to Panel and Arc Menu. This is not a duplicate of a previous question. This specifically about automatically restarting Gnome Shell after coming out from suspend. Questions are totally different.

Edit: following the suggestions on comments:

a. Created a file on /etc/systemd/system called restart-gnome-shell.service and add the following:

[Unit] 
Description=Restart Gnome-Shell
Before=sleep.target
StopWhenUnneeded=yes

[Service] 
Type=oneshot 
RemainAfterExit=yes 
ExecStop=-/path/to/script.sh

[Install] 
WantedBy=sleep.target

The script.sh contents are:

#! /bin/bash
gnome-shell --replace

The script has execution permissions and I enabled and started the service which was recognized by systemctl. This did not work.

This is the status after waking up from suspend:

● restart-gnome-shell.service - Restart Gnome-Shell
   Loaded: loaded (/etc/systemd/system/restart-gnome-shell.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

jul 08 12:51:07 Enrique-PC systemd[1]: Started Restart Gnome-Shell.
jul 08 15:53:08 Enrique-PC systemd[1]: restart-gnome-shell.service: Unit not needed anymore. Stopping.
jul 08 15:53:08 Enrique-PC systemd[1]: Stopping Restart Gnome-Shell...
jul 08 15:53:08 Enrique-PC restart-gnome-shell.sh[2119]: Window manager warning: Unsupported session type
jul 08 15:53:08 Enrique-PC systemd[1]: Stopped Restart Gnome-Shell.

b. Created an executable script on /lib/systemd/system-sleep with the following contents:

case "${1}" in
    post)
         gnome-shell --replace
;;
esac

It did not work.

eera5607
  • 367
  • You can try this: https://askubuntu.com/questions/92218/how-to-execute-a-command-after-resume-from-suspend with the command gnome-shell --replace. – pomsky Jul 04 '18 at 18:20
  • @pomsky thanks, but that command never completes because it says for example that Dropbox client is already registered and never continues. Similar issues with other Ubuntu AppIndicators. – eera5607 Jul 04 '18 at 18:30
  • 1
    Possible duplicate of Color distortion after coming out from suspend on Ubuntu 18.04. Please edit that question with the additional information. – user535733 Jul 04 '18 at 18:34
  • You may have a GNOME extension conflict. Please edit your question to list your set of enabled GNOME extensions shown at https://extensions.gnome.org/local/ Report back to @heynnema – heynnema Jul 04 '18 at 18:48
  • @heynnema thanks, I edited the question with the installed extensions. – eera5607 Jul 04 '18 at 19:26
  • 1
    @user535733 This is not a duplicate of a previous question. This is specifically about automatically restarting Gnome Shell after coming out from suspend. Questions are different. – eera5607 Jul 04 '18 at 19:46
  • Temporarily turn off these two extensions, and retest the suspend problem. Report back. – heynnema Jul 04 '18 at 19:57
  • @heynnema thanks. The problem is not with suspension. That was the other question that I mentioned at the beginning of this one. My problem is with the suggested command to restart Gnome Shell and even after disabling those extensions, the command gnome-shell - - replace never finishes to work on terminal and if I stop it everything freezes. – eera5607 Jul 04 '18 at 20:23
  • I think the command that you want ends in -r, or --replace (no spaces). What video driver are you using? – heynnema Jul 04 '18 at 20:47
  • @heynnema sorry yes, I was answering in the phone and did not noticed that it added spaces, but I'm using the one without the spaces. The graphics card is Caicos [Radeon HD 6450/7450/8450 / R5 230 OEM] Advanced Micro Devices, Inc. [AMD/ATI]. I'm using the default driver that came with Ubuntu 18.04. I have no option to change it under Additional drivers. – eera5607 Jul 04 '18 at 21:24
  • @pomsky thanks, I'm not sure if it completes the Gnome Shell restart because if I execute it directly on terminal it never completes and you said it is supposed to complete. – eera5607 Jul 06 '18 at 04:19
  • Sorry, I typed it wrong. I meant that command is not supposed to complete. – pomsky Jul 06 '18 at 05:01
  • This looks like your problem, although it is already old: https://unix.stackexchange.com/questions/13029/how-can-i-automatically-restart-gnome-shell-coming-out-of-suspend-mode – vanadium Jul 06 '18 at 08:53
  • @pomsky thank you, in that case yes, it seems to be working. I will try your solution with that command then. – eera5607 Jul 06 '18 at 21:57
  • @vanadium yes I already looked at that question but apparently It does not work on newer versions of Ubuntu. – eera5607 Jul 06 '18 at 21:57
  • Perhaps try this: https://mariogalan.com/en/content/execute-script-after-suspend-ubuntu-1604, which suggests adding a script to /lib/systemd/system-sleep to perform an action on wake. – vanadium Jul 07 '18 at 13:37
  • @pomsky I added to the question the process that I follow according to the link that you shared. Can you please check if I did anything wrong. – eera5607 Jul 08 '18 at 22:22
  • @vanadium thanks, I also tried with the solution that you shared but it did not work. I also added what I did to the question. Can you please check? – eera5607 Jul 08 '18 at 22:25
  • @pomsky thanks. Yes, in the question I use /path/to/script.sh as an example but I used the correct path. And yes, I enabled the service. – eera5607 Jul 09 '18 at 03:28
  • Did you ever get this fixed? I am trying the same things. – Ryan David Ward Apr 10 '19 at 09:01
  • @RyanWard noup... But it only happens if the PC suspends itself after the specified time not if you manually suspend it. I don't understand why. – eera5607 Apr 17 '19 at 17:54

2 Answers2

2

Change your script from this:

case "${1}" in
    post)
         gnome-shell --replace
;;
esac

To this:

#!/bin/sh

case $1/$2 in
  pre/*)
    echo "Going to $2..."
    # Place your pre suspend commands here, or `exit 0`
    # if no pre suspend action required
    sleep 1
    ;;
  post/*)
    echo "Waking up from $2..."
    # Place your post suspend (resume) commands here, or `exit 0` 
    # if no post suspend action required
    sleep 2
    gnome-shell --replace
    ;;
esac

For sure you are missing #!/bin/sh at the top of your script. Most of the rest of the proposed changes are not be necessary but informative.

  • Thank you. I don't know why it is not working. I used the script that you suggest, copy it to /lib/systemd/system-sleep/ and nothing. – eera5607 Jul 09 '18 at 04:27
0

I know this reply is a bit late, but I will post my method, maybe it will be useful to someone :-). !!! Only works with Xorg!!!.

The solution I found (a trick) is to create a script in /lib/systemd/system-sleep that stores a value ("1") in a file every time it wakes up from sleep, and I have another script that is always running that checks the contents of that file and when it is equal to "1" it runs the shell restart code and then changes the contents of the file to "0". The code to restart the shell is also a "trick" as I haven't found a command to do this, where I have to use xdotool to simulate keypress and mouse click.

Scripts:

  • /lib/systemd/system-sleep/resume_status
#!/bin/sh

if [ "${1}" == "post" ]; then sleep 5 echo "1" > PATH-TO-FILE/FILE fi

sleep 5 is used to wait the system to enter graphical environment

  • a user script to check the resume from sleep (placed somewhere in your HOME) :
#!/bin/bash

while true; do check=$(sed '1q;d' PATH-TO-FILE/FILE) if [[ "$check" == "1" ]]; then xdotool key alt+F2 sleep 1 xdotool click 1 xdotool type 'r' xdotool key Return echo "0" > PATH-TO-FILE/FILE fi sleep 5 done

Change PATH-TO-FILE/FILE to match your case and make sure xdotool is installed.

I have been using this method for a while now and it is working fine.