Hibernate from the command line or from a script

It has taken Linux a while before hibernate support became available and to work reliable. The distribution I run is OpenSUSE and somwehere since version 10 dot something it the hibernate support works well.

What I don't like is that it takes too long to go to the hibernation from the menu and that there are processes that I would like to sto pbefore hibernating. An example is virtual machines. If I have one running in VMware Workstation I really prefer to have them suspended before I hibernate my computer. And I want my broadband connection to be stopped before hibernating.

With the simple script below added to your taskbar or panel you can hibernate much easier.

Note: the example below works on SUSE Linux, you migth need to tweak commands to your distro.

  1. Make sure that your user account is allowed to perform pm-hibernate, normally only root can do that. For this to work add the folowing line to your /etc/sudoers file:

    username    ALL = NOPASSWD: /usr/sbin/pm-hibernate

  2. Here is the actual script to start the hibernate: (I will explain the details below)

    #!/bin/bash
    if (ps -ae | grep pppd)
      then
      killall umtsmon ;
      killall pppd ;
    fi
    vmrun -T ws list | grep vmx | xargs -I {} vmrun -T ws suspend {}
    sudo /usr/sbin/pm-hibernate &
    gnome-screensaver-command -a -l

  3. The first thing to do is check if my broadband connection is live (pppd loaded my UMTSMON and if so kill those processes.
  4. I had already explained that I want to suspend any running virtual machines. With the command: vmrun -T ws list I list all running virtual machines. I then send it through a pipe to grep to make sure I only process lines that contain a valid virtual machine configuration file (with .vmx in the file name) and thus skip vmrun's header line in the output. Finally I send that through a pipe to the xargs command that executes the command vmrun -T ws suspend for each virtual machine that was found.
  5. Then it's time to call the pm-hibernate command. When I use that command the strange behaviour is that when returning from hibernate the screen isn't locked. Which is the case when I select hibernate from the actual shutdown menu because asking for the password is an option I have enabled in the power settings. To change this behaviour I run pm-hibernate in the background and run the command gnome-screensaver-command -a -l to enable the screensaver and lock the screen. This then happens right before the actual hibernate is performed and the result is that the machine is locked when returning from hibernate.
  6. And then the last step is to be able to execute the script easily. I have created a custom launcher in my panel:

Any questions or comments? Feel free to contact me.