It's also important to make sure that there is only one instance of the script running at a time, otherwise things can get chaotic.
So, assuming the script is called suspend-jobs , this is the final form of the script:
#!/bin/sh
# kill other instances of this script:
kill $(pgrep -f suspend-jobs | grep -v $$) 2>/dev/null
# pre-suspend jobs:
foo
bar
baz
# suspend:
sudo sed -i '/resume complete/ d' /var/log/messages
sudo sh -c "echo mem >/sys/power/state"
# wait for system resume to be complete:
while true; do
grep -q "resume complete" /var/log/messages 2>/dev/null && break
sleep 0.1
done
# post-suspend jobs:
qux
quux
quuz
Thread may be marked as "Solved". Thank you both very much.
P.S. If anyone can think of further improvements to boost reliability, please do share.
EDIT: I changed the script so that it doesn't delete /var/log/messages before suspending.