Thursday, 20 November 2014

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

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.

cat >> ~/bin/ << EOF

# 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/ 7200

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

#3600secs = 60min * 60sec = 1 hour
[[ "$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

# make the script executable
chmod 755 ~/bin/

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

Thanks to 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: