Background
I'm trying to restart some programs (mail-notification and stalonetray) regularly, as they appear to die frequently. I want to set restart them whenever NetworkManager reconnects. Hence, I have them triggered by a script in /etc/NetworkManager/dispatcher.d/.
Scripting
I can create a script as follows.
#!/bin/bash
sudo -u foo_user pkill mail-notificati -x
sudo -u foo_user DISPLAY=:0 mail-notification &
This works fine if I run it directly as a user. However, if I call it from root's script, it fails. I am prompted to enter the passwords for mail-notification; it cannot read Gnome Keyring.
How can I run this program as foo_user in every way?
DISPLAYshould be set for it to be "in every way" doesn't make much sense. You'd need to define this question more for it to make sense.rootas it does when running the script asfoo_user. I appreciate thatDISPLAYisn't necessarily relevant here, but included it as an example of what I was doing.mail-notificationprocess as infoo_user's environment.foo_useris logged in, and on which display? On a single-user system it's perhaps reasonable to assume that it's always:0.0but it is not reasonable to assume that the user is logged in at all times. Anyway, this makes more sense to run within the X session script offoo_user, which will remove both your original problem and the complications it caused you to want to try to solve.