Hund

How to automatically run a resume script with pm-utils as a regular user

June 1, 2020

I’m using Gentoo with OpenRC and I use the tool pm-utils to suspend my computer. The only issue I have with their way of automatically running a command when you resume your computer is the fact that it’s executed as root.

The command

If you want to run a command as any user you easily do that by using sudo:

$ sudo -H -u <user> sh -c "<command>"

Here’s an explanation of the flags I’m using:

-H, --set-home Request that the security policy set the HOME environment variable to the home directory specified by the target user’s password database entry. Depending on the policy, this may be the default behavior.
-U user, --other-user=user Used in conjunction with the -l option to list the privileges for user instead of for the invoking user. The security policy may restrict listing other users’ privileges. The sudoers policy only allows root or a user with the ALL privilege on the current host to use this option.

The script

Here’s an example script, it’s one of my scripts called 00refresh:

#!/bin/bash
case $1 in
    suspend|hibernate)
    ;;
    resume|thaw)
        sudo -H -u johan sh -c "DISPLAY=:0.0 /home/johan/Scripts/pm-resume.sh"
    ;;
    *)  exit $NA
    ;;
esac

The script is placed in the folder /etc/pm/sleep.d/ and made executable. The number 00 in the beginning of the filename defines the sleep hook ordering convention. When you set it to something between 00 and 49 it assumes the usual services and userspace infrastructure is still running, you can read more about that in the manual.

Meta

No Comments

Use the e-mail form, if you wish to leave feedback for this post. Markdown is supported. [Terms of service]