How to automatically run a resume script with pm-utils as a regular user
Published: Monday, 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.
If you want to run a command as any user you easily do that by using
$ sudo -H -u <user> sh -c "<command>"
Here’s an explanation of the flags I’m using:
||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.|
||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.|
Here’s an example script, it’s one of my scripts called
#!/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.
If you want to leave feedback you can do so by either sending me a message via e-mail or by commenting on my post for this article on the fediverse.