#!/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?
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_~$
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 .
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?
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.
#!/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 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
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.