0

The following answer shows how to run a script after waking from suspend based on systemd. I tried them but could not get a gnome-terminal to appear to run a command.

#!/bin/sh
if [ "$1" == "post" ]; then
  gnome-terminal --command 'compiz --replace &' 

Why did they not work? I am using Ubuntu 16.04.2 kernel 4.8.0-45-generic. I am curious why my systemd based script did not work. My command works on an opened gnome-terminal but somehow won't work with systemd-suspend.service method. ???

Sun Bear
  • 2,302

1 Answers1

0

As you know any terminal emulators have been closed after doing their jobs.

But if you want to gnome-terminal not to be closed using this script in /etc/pm/sleep.d path (this script cause to keep terminal opened until you hit enter) :

sudo nano /etc/pm/sleep.d/myscript

Then paste these content to you script :

#!/bin/bash
case $1 in 
     thaw|resume) gnome-terminal --command 'bash -c "compiz --replace &; read line"'
     ;;
esac

Then make it executble by this command:

sudo chmod a+x /etc/pm/sleep.d/myscript

I hope that's work for you.

UPDATE:

Thanks for sharing. I was curious why my systemd based script did not work. My command works on an opened gnome-terminal but somehow won't work with systemd-suspend.service solution. Could you try on your system to see it has any effect? You can replace the compriz command with command "sleep 20" to try. Appreciate your feedback The suspend way is different on many machines some of them use sudo pm-suspend and some of them use systemctl suspend command.

We can use systemd based script if we use systemctl suspend for OS sleep (not to use sudo pm-suspend. but in some systems, when you click on suspend boton, the operating system use pm-suspend command). But the systemd based scripts and powermanagement based scripts have a little difference with each other:

In systemd based script we can't run GUI applications because in that condition there is no X server for it to connect to.

So you can't use gnome-terminal in such commands like gnome-terminal --command 'compiz --replace &' . if you want to show gnome-terminal you must use powermanagement based scripts.

But in your case it's enough to use this script (without gnome-terminal):

#!/bin/sh
if [ $1 = post ] ; then
compiz --replace &
fi

And you can test your script by replacing something like touch /home/YOURUSERNAME/Desktop/test.txt instead of compiz --replace &:

#!/bin/sh
if [ $1 = post ] ; then
touch /home/YOURUSERNAME/Desktop/test.txt
fi

(Be aware that because in systemd based scripts the root user run commands so do not use ~/Desktop instead of /home/YOURUSERNAME/Desktop because the the root user's ~ is not your home directory !)

  • Thanks for sharing. I was curious why my systemd based script did not work. My command works on an opened gnome-terminal but somehow won't work with systemd-suspend.service solution. Could you try on your system to see it has any effect? You can replace the compriz command with command "sleep 20" to try. Appreciate your feedback. – Sun Bear Apr 04 '17 at 07:13
  • I have edited my comment and answered your question – Ali Razmdideh Apr 04 '17 at 19:23
  • Thanks @ali . I found that compiz --replace& solves my display issues only when executed from a terminal. Hence, I used gnome-terminal to issue the command. You might want to re-check. Noted on the diff between systemd and powermanagement. ;) – Sun Bear Apr 08 '17 at 06:11
  • @Sun Bear . you can use ~/.xsessionrc and ~/.xsession directories instead of that ; but as far as i know U can't run run gui applications with systemd base script – Ali Razmdideh Apr 08 '17 at 06:58