33

I managed to find out how to suspend/hibernate the system from the command line by reading How can I suspend/hibernate from command line?.

However, I would like to know how to use the command line to suspend/hibernate at a given time, in absolute (example, 11PM) or relative (example, in 30 minutes) terms.

I already know how to do this with shutdown, but I'm note sure if the command is similar.

41

You can use the at command to schedule any action, including running the commands detailed in that question.

For example, if you want to hibernate in 30 minutes:

echo 'pmi action hibernate' | at now + 30 min

Or if you want to suspend at 11:00 pm:

echo 'pmi action suspend' | at 11pm

If you need to run a command as root, run at with sudo rather than the command itself with sudo (since sudo should only be run interactively, unless you've configured it not to ask for your password). For example, the equivalents of the above commands using pm-hibernate and pm-suspend are:

echo pm-hibernate | sudo at now + 30 min

echo pm-suspend | sudo at 11pm

  • Thanks for catching that both situations were asked for... I edited the title to reflect this as well. I modified my answer to include both situations now. – cprofitt Nov 13 '11 at 20:37
  • Any idea how I can clear the jobs? – Rexford Oct 11 '16 at 3:09
  • 1
    @Rexford The atrm command removes jobs created by at. – Eliah Kagan Dec 27 '16 at 15:15
6

For relative specification (e.g. "after 30 minutes") you can simply use sleep command to make suspending/hibernating command wait.


Examples:

Wait 30 minutes, then suspend:

sudo sleep 30m; sudo pm-suspend

Wait 1 hour, then hibernate:

sudo sleep 1h; sudo pm-hibernate
  • I like this approach too. – Rexford Oct 11 '16 at 3:08
  • 1
    After 1h pm-hibernate want a password for root again and suspend does not happen. – Vitaly Zdanevich Oct 5 '17 at 4:46
  • how about logging in as root to avoid getting asked for a password? – banan3'14 Jan 26 at 9:54
4

For specific times repeated - like shutting down computers are a specific time each day. use cron.

crontab -e

add the following:

15 14 1 * * pmi action suspend

If you want to customize it.

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

For a one time job us the at command

For example, if you want to hibernate in 30 minutes:

echo 'pmi action hibernate' | at now + 30 min

Or if you want to suspend at 11:00 pm:

echo 'pmi action suspend' | at 11pm
  • 2
    Using cron is a good solution when the goal is to suspend or hibernate at a specific time of day, or a specific time on a specific day of the week, or month, or year. But for scheduling a single suspension or hibernation, which seems to be what Ederico was asking about (and which is something that makes a lot more sense to want to do), the at command should be used. (You can schedule a single event with cron by making the event edit the relevant crontab, for example using sed or awk, but that's extremely complex and unnecessary; use at.) – Eliah Kagan Nov 13 '11 at 19:49
  • Does not fit to the relative terms requested by the question, its only useful for specific schedules. – Bruno Pereira Nov 13 '11 at 19:50
  • Yes, this was an example for the 'specific time' that was requested. "However, I would like to know how to use the command line to suspend/hibernate at a given time, in absolute (example, 11PM)" – cprofitt Nov 13 '11 at 20:23
  • 1
    You've edited your answer to add information about how to use at, but the information you have added is unfortunately not correct. The < operator redirects text from the file or device specified to its right to standard input for the command specified to its left. If you wanted to use < instead of | (as in my answer), you'd have to make a file with pmi action suspend (or pmi action hibernate) as its contents. Such complexity makes | better here. – Eliah Kagan Nov 13 '11 at 21:02
  • As a general guideline, when you have a cat command to the left of a | (e.g. cat foo | bar), you can replace that with a < expression (bar < foo). But when you have an echo command to the left of a |, that cannot be trivially replaced with a < expression. – Eliah Kagan Nov 13 '11 at 21:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.