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. – Chris Down Sep 29 '15 at 05:14rootas 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. – Sparhawk Sep 29 '15 at 05:16mail-notificationprocess as infoo_user's environment. – Sparhawk Sep 29 '15 at 05:18foo_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. – tripleee Sep 29 '15 at 06:48foo_useris logged in, and theDISPLAYis correct (and it works fine withstalonetray). I suspect I'm missing something else. Could you please provide more information on how to run it within the X session script? That sounds promising. – Sparhawk Sep 29 '15 at 07:08$HOME/.xsessionscript; modern desktop managers typically allow you to click and drool an executable script to be added to the user's startup actions. The script itself could just be a simple loop which wakes up every n seconds and checks if something needs to be relaunched. – tripleee Sep 29 '15 at 08:16sudo -i? – Jeff Schaller Sep 29 '15 at 12:28foo_user's crontab. However, I was more curious in a general solution, which might be triggered by root activities such as NetworkManager hooks, udev, pm-suspend (resume), etc. – Sparhawk Sep 29 '15 at 22:29sudo sudo -i -u foo_user my_script, but it still couldn't access Gnome Keyring – Sparhawk Sep 29 '15 at 22:39envas foo_user versussudo -i -u foo_user env. – Jeff Schaller Sep 30 '15 at 17:29