I want to run a 4 commands before a hibernate and another 2 commands after update. Is it also possible to arrange the time/squence of each commands,if yes then could you explain how?

| improve this question | | | | |

You can run commands before and after hibernation or suspension (note there is a difference; hibernation is to disk, suspension is to memory) by creating a script in /etc/pm/sleep.d:


case "$1" in
    # put commands to run on hibernation here
    # put commands to run when returning from hibernation here
    # put commands to run on suspend here
    # put commands to run when returning from suspension

The filename of the script will dictate the order in which the scripts run compared to other scripts in sleep.d. Within your script, your commands will run in whatever order you put then in the script.

| improve this answer | | | | |
  • Could you explain why the filename has to be 'xxname' where x is a number, what does x mean? – user128712 Jun 15 '13 at 4:45
  • 2
    The files are ordered (and therefore executed) in alpha-numeric order. The number is not necessary, but it provides a way of controlling the order of the scripts. – David Edwards Jun 15 '13 at 9:11
  • Please note that every command in the xxname file will be executed as root. So if you need to stop/start some non-system application (for example, Dropbox) run corresponding command as usual user with aid of su (for example, su --command 'dropbox start' user. – flaz14 Jul 16 '16 at 14:37
  • 2
    With the change to systemd, this answer is only correct for 14.04 and earlier. See Make a script start after suspend in Ubuntu 15.04 (systemd) and man systemd-sleep for later versions. – AgentB Oct 30 '16 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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