Debug script

How can I debug this script?

I want to know what it is doing or not doing?

#!/bin/bash
#
# 
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
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi

Given that we have no access to your system and can't see any diagnostic messages produced while your script is running, that you have not said anything about what you think might not be working in your script, and have not told us what appears in the file named /tmp/systemd_suspend_test.txt after you run your script; I find it difficult to suggest what you might do differently to debug your code.

I guess one might ask why you're throwing away all of the debugging data you have collected whenever your script is invoked with the first operand set to pre ?

I guess one might ask why you don't include any timestamps in your debugging output?

I guess one might ask why you don't include a set -vx command at the start of your script and save the diagnostic information that would produce in a log file?

Use:

 #/bin/bash -x

The script use two parameters:

  1. ./script pre
  2. ./script post

Nothing appears in ]/tmp/systemd_suspend_test.txt. Because if it did, the script would be working. :slight_smile:

As to your 1st guess, I have no idea how to implement what you are asking. i.e. The reason for my post.

Same with your 2nd and 3rd guesses.

--- Post updated at 03:11 AM ---

I added -x.

Nothing new happened.

My code is a modification of what is here.

Running scripts before and after suspend with systemd << Just another Linux geek

What output do you get from the command:

ls -l /tmp/systemd_suspend_test.txt

?
What output do you get from the command:

id

?
What output do you get from the command:

bash -xv /your/script/name pre

where /your/script/name is an absolute pathname of your script? After running this command what output do you get from the command:

ls -l /tmp/systemd_suspend_test.txt

?
What output do you get from the command:

bash -xv /your/script/name post

where /your/script/name is an absolute pathname of your script? After running this command what output do you get from the command:

ls -l /tmp/systemd_suspend_test.txt

?

andy@7_~$ ls -l /tmp/systemd_suspend_test.txt
ls: cannot access '/tmp/systemd_suspend_test.txt': No such file or directory
andy@7_~$ id
uid=1000(andy) gid=1000(andy) groups=1000(andy),4(adm),20(dialout),21(fax),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)
andy@7_~$ bash -xv /usr/lib/systemd/system-sleep/On_Resume pre
#!/bin/bash #-x
#
# 
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
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi
+ '[' pre = pre ']'
+ echo 'we are suspending.'
andy@7_~$ ls -l /tmp/systemd_suspend_test.txt
-rw-rw-r-- 1 andy andy 19 Apr  2 03:45 /tmp/systemd_suspend_test.txt
andy@7_~$ bash -xv /usr/lib/systemd/system-sleep/On_Resume post
#!/bin/bash #-x
#
# 
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
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi
+ '[' post = pre ']'
+ '[' post = post ']'
+ echo 'and we are back from being suspended'
andy@7_~$ ls -l /tmp/systemd_suspend_test.txt
-rw-rw-r-- 1 andy andy 56 Apr  2 03:46 /tmp/systemd_suspend_test.txt
andy@7_~$ 

So your script seems to do what you want it to do if it is invoked by you.

How have you installed your script so the system will invoke it before suspending your system and while resuming your system after a suspension?

According to one of the comments in the link you posted, the correct directory for this on a Debian, and therefore Ubuntu system, is /lib/systemd/system-sleep , not /usr/lib/systemd/system-sleep .

Did you have to create the directory /usr/lib/systemd/system-sleep , or was it there when you started?

Does the directory /lib/systemd/system-sleep exist? What happens if you put your script there?

Did you remember to set the execute attributes on the script?

Try adding the line

exec 2> /tmp/systemd_suspend_test_err.txt

to the beginning of your script. That should redirect trace output to the file /tmp/systemd_suspend_test_err.txt .

Andrew

1 Like

The script is in /usr/lib/systemd/system-sleep/.

As per

Running scripts before and after suspend with systemd << Just another Linux geek

It also has the execute bit set.

I found this in syslog.

It seems odd. (The scripts also has no sh extension.)

Apr  2 06:03:48 7 systemd-sleep[5614]: /lib/systemd/system-sleep/On_Resume.sh: 2: [: post: unexpected operator
Apr  2 06:03:48 7 systemd-sleep[5614]: /lib/systemd/system-sleep/On_Resume.sh: 5: [: post: unexpected operator

Read Sampi's response:

So you should be using /lib/systemd/system-sleep/ as you are using Ubuntu, which is a Debian-based system. If you are not using Ubuntu, why are you posting to an Ubuntu sub forum?

Andrew

Absolutely! I was about to write the same, you beat me to it.

Exactly what apmcd47 already said: /lib/systemd/system-sleep/On_Resume.sh is not the same as /usr/lib/systemd/system-sleep/On_Resume.sh and you are simply debugging the wrong script.

I hope this helps

bakunin

I put script in correct directory.

No change.

I found this in that dir.

Format looks different.

#!/bin/sh
set -e

if [ "$2" = "hibernate" ] || [ "$2" = "hybrid-sleep" ]; then
    case "$1" in
        pre)
            /usr/share/unattended-upgrades/unattended-upgrade-shutdown --stop-only
            ;;
    esac
fi

I am not sure what that means: in post #6 you said your script is named /usr/lib/systemd/system-sleep/On_Resume . You need to not only move it to another directory (which would make it /lib/systemd/system-sleep/On_Resume ) but also name it differently: /lib/systemd/system-sleep/On_Resume.sh Have you done both? If yes, there should at least be some message in /tmp/systemd_suspend_test_err.txt now, otherwise your script has most probably NOT run.

It not only looks differently - it is! It does something completely different: first, this script expects TWO parameters, not just one. If the second is not "hibernate" or "hybrid-sleep", it does simply nothing at all. Second, this script is most probably run by a different shell: /bin/sh instead of bash . Depending on what /bin/sh links to (in Ubuntu perhaps dash ) this will not be dash . Third, even if this script would do something (which is now, given the parameters with which you called the first script, unlikely) it would be something completely different from what the first script does.

I hope this helps.

bakunin

I thought I posted that my problem was not setting the exe bit, but I could not find the post.

The script does run.

I added this, but it does not run.

I gave the full path.

/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  'Back from being suspended.'

I guess enquiring minds want to know...

How do you know that your script is running?

Exactly where did you add the above code in your script? (I.e., What does your complete script look like now?)

What is invoking your script and what arguments are being passed to your script when it is invoked?

1 Like

I know script is running because it creates the text file in the tmp directory.

#!/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
  /usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  'Fixing to suspend computer.'
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here
  sleep 5
  echo "and we are back from being suspended" >> /tmp/systemd_suspend_test.txt
  /usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  'Back from being suspended.'
fi

Just for the fun of it, please change the following line in your script:

exec 2> /tmp/systemd_suspend_test_err.txt

to:

exec 2>> /tmp/systemd_suspend_test_err.txt
date >&2
printf "$0 argument: %s\n" "$@" >&2

and then show us what appears in /tmp/systemd_suspend_test_err.txt after your script is invoked by the system.

1 Like

I got this in
systemd_suspend_test_err.txt

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

With your changes

Tue Apr  2 19:55:04 CDT 2019
/lib/systemd/system-sleep/On_Resume argument: pre
/lib/systemd/system-sleep/On_Resume argument: suspend
Unable to init server: Could not connect: Connection refused
gxmessage: unable to initialize GTK
Tue Apr  2 19:55:11 CDT 2019
/lib/systemd/system-sleep/On_Resume argument: post
/lib/systemd/system-sleep/On_Resume argument: suspend
Unable to init server: Could not connect: Connection refused
gxmessage: unable to initialize GTK

I'm not familiar with gxmessage , but from what we are seeing in the log, one might guess that some service it depends upon has already been shutdown before your script is called when the system is suspending and that the service hasn't been restarted yet before your script is called when the system is coming back up.

At least now we know what arguments are being supplied to your script when it is invoked.

Don't forget to remove your temp files after you have examined what happened after your script is invoked.

1 Like

I thought the temp files were automatically deleted upon reboots.?