# Debug script

**URL:** <https://community.unix.com/t/debug-script/375294>\
**Category:** Ubuntu\
**Created:** [April 1, 2019, 5:38pm UTC](https://community.unix.com/t/debug-script/375294 "2019-04-01T17:38:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 1, 2019, 5:38pm UTC](https://community.unix.com/t/debug-script/375294/1 "2019-04-01T17:38:17Z")

</div>

How can I debug this script?

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

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 2, 2019, 12:53am UTC](https://community.unix.com/t/debug-script/375294/2 "2019-04-02T00:53:50Z")

</div>

> [@drew77](#):
>
> How can I debug this script?
> 
> I want to know what it is doing or not doing?
> 
> ```plaintext
> #!/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?

---

<div class="post-metadata">

**Author:** ![nelbren](https://community.unix.com/user_avatar/community.unix.com/nelbren/32/10135_2.png) [@nelbren](https://community.unix.com/u/nelbren)\
**Post date:** [April 2, 2019, 3:30am UTC](https://community.unix.com/t/debug-script/375294/3 "2019-04-02T03:30:45Z")

</div>

Use:

```nohighlight
 #/bin/bash -x

```

The script use two parameters:

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

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 4:11am UTC](https://community.unix.com/t/debug-script/375294/4 "2019-04-02T04:11:15Z")

</div>

> [@don cragun](#):
>
> 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?

Nothing appears in ]/tmp/systemd\_suspend\_test.txt. Because if it did, the script would be working. 🙂

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 ---

> [@nelbren](#):
>
> Use:
> 
> ```plaintext
> #/bin/bash -x
> 
> ```
> 
> The script use two parameters:
> 
> - ./script pre
> - ./script post

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](https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/)

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 2, 2019, 4:30am UTC](https://community.unix.com/t/debug-script/375294/5 "2019-04-02T04:30:16Z")

</div>

What output do you get from the command:

```nohighlight
ls -l /tmp/systemd_suspend_test.txt

```

?  
What output do you get from the command:

```nohighlight
id

```

?  
What output do you get from the command:

```nohighlight
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:

```nohighlight
ls -l /tmp/systemd_suspend_test.txt

```

?  
What output do you get from the command:

```nohighlight
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:

```nohighlight
ls -l /tmp/systemd_suspend_test.txt

```

?

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 4:47am UTC](https://community.unix.com/t/debug-script/375294/6 "2019-04-02T04:47:13Z")

</div>

```nohighlight
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_~$ 

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 2, 2019, 5:31am UTC](https://community.unix.com/t/debug-script/375294/7 "2019-04-02T05:31:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![apmcd47](https://community.unix.com/letter_avatar/apmcd47/32/6_449c533f586939b6e5f246632a46421f.png) [@apmcd47](https://community.unix.com/u/apmcd47)\
**Post date:** [April 2, 2019, 5:54am UTC](https://community.unix.com/t/debug-script/375294/8 "2019-04-02T05:54:11Z")

</div>

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

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 6:56am UTC](https://community.unix.com/t/debug-script/375294/9 "2019-04-02T06:56:16Z")

</div>

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

As per

[Running scripts before and after suspend with systemd \<\< Just another Linux geek](https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/)

It also has the execute bit set.

I found this in syslog.

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

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![apmcd47](https://community.unix.com/letter_avatar/apmcd47/32/6_449c533f586939b6e5f246632a46421f.png) [@apmcd47](https://community.unix.com/u/apmcd47)\
**Post date:** [April 2, 2019, 7:06am UTC](https://community.unix.com/t/debug-script/375294/10 "2019-04-02T07:06:08Z")

</div>

> [@drew77](#):
>
> The script is in /usr/lib/systemd/system-sleep/.
> 
> As per
> 
> [Running scripts before and after suspend with systemd \<\< Just another Linux geek](https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/)
> 
> It also has the execute bit set.

Read [Sampi's response](https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/#comment-974077):

> [@](#):
>
> Other users of Debian might want to take notice that the path to place the files under for that distribution is /lib/systemd/system-sleep/, i.e. without the initial /usr. Although this actually is properly documented with the correct path in the man page, it is a detail which is easy to miss.

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

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [April 2, 2019, 7:15am UTC](https://community.unix.com/t/debug-script/375294/11 "2019-04-02T07:15:36Z")

</div>

> [@apmcd47](#):
>
> Read [Sampi's response](https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/#comment-974077)

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

> [@drew77](#):
>
> The script is in /usr/lib/systemd/system-sleep/.  
> I found this in syslog.
> 
> ```plaintext
> 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
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 7:39am UTC](https://community.unix.com/t/debug-script/375294/12 "2019-04-02T07:39:49Z")

</div>

I put script in correct directory.

No change.

I found this in that dir.

Format looks different.

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [April 2, 2019, 8:02am UTC](https://community.unix.com/t/debug-script/375294/13 "2019-04-02T08:02:04Z")

</div>

> [@drew77](#):
>
> I put script in correct directory.

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.

> [@drew77](#):
>
> Format looks different.
> 
> ```plaintext
> #!/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
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 7:33pm UTC](https://community.unix.com/t/debug-script/375294/14 "2019-04-02T19:33:05Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 2, 2019, 8:21pm UTC](https://community.unix.com/t/debug-script/375294/15 "2019-04-02T20:21:20Z")

</div>

> [@drew77](#):
>
> 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.
> 
> ```plaintext
> /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?

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 8:23pm UTC](https://community.unix.com/t/debug-script/375294/16 "2019-04-02T20:23:36Z")

</div>

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

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 2, 2019, 8:49pm UTC](https://community.unix.com/t/debug-script/375294/17 "2019-04-02T20:49:20Z")

</div>

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

```nohighlight
exec 2> /tmp/systemd_suspend_test_err.txt

```

to:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 8:53pm UTC](https://community.unix.com/t/debug-script/375294/18 "2019-04-02T20:53:25Z")

</div>

I got this in  
systemd\_suspend\_test\_err.txt

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

```

With your changes

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 2, 2019, 9:08pm UTC](https://community.unix.com/t/debug-script/375294/19 "2019-04-02T21:08:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [April 2, 2019, 9:53pm UTC](https://community.unix.com/t/debug-script/375294/20 "2019-04-02T21:53:26Z")

</div>

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

[Next page](https://community.unix.com/t/debug-script/375294.md?page=2)
