Thursday 20 November 2014

I want to go to bed but must wait for something to finish on my computer - ~/bin/sleepandsuspend.sh

If I'm doing a big upload or download or sending a big email or doing some other lengthy task but I wish to GO TO BED. Then can I allow it time to finish the task but make it suspend after say an hour?
Yes. This is working for me. (ubuntu linux)

Easiest method:
sudo bash -c "sleep 1h; pm-suspend"

OR a more protecting script to avoid basic mistakes like NOT RUNNING AS ROOT. or NOT HAVING pm-suspend. duh. Good for sleepy users who want to go to bed.
https://gist.github.com/Gaoithe/186ba4adaca66f2e020c


cat >> ~/bin/sleepandsuspend.sh << EOF
#!/bin/bash

# to run it optionally give it amount of seconds to wait
# e.g. two hours 7200, e.g. 10 mins 600
echo e.g. invoke: sudo ~/bin/sleepandsuspend.sh 7200

[[ "$USER" != "root" ]] && echo must run this as root. && exit 0;

#3600secs = 60min * 60sec = 1 hour
SLEEP=3600
[[ "$1" != "" ]] && SLEEP=$1

which pm-suspend
[[ "$?" != 0 ]] && echo "Oh dear, I cannot run pm-suspend. This script will not work :-7"

sleep $SLEEP && pm-suspend
# pmi action suspend
EOF

# make the script executable
chmod 755 ~/bin/sleepandsuspend.sh

# to run it optionally give it amount of seconds to wait, e.g. two hours 7200
sudo ~/bin/sleepandsuspend.sh 7200


Thanks to askubuntu.com users for the suspend commands

# If you knew a certain process will finish (e.g. a top-level make process) then you can use ps -elf // pstree -anp // whatever to get the pid of the process which is taking a long time. This is not possible though with some things like a google chrome window sending a big email. You can use wait to wait until the process exits. 
# PID is the  pid number OR %jobid of  process you think will exit
# Invoke like this:
sudo bash -c "wait && pm-suspend"


No comments: