I would like to execute an application (gnutls-cli) as another user and send a signal to it (SIGALRM). Unfortunately this does not work:

sudo -u myuser gnutls-cli -p smtp imap.gmail.com --starttls &
sudo -u myuser kill -ALRM $!

gnutls-cli is a child of the sudo process, that is I got the following process tree:

\_ sudo -u myuser gnutls-cli -p smtp imap.gmail.com --starttls
    \_ gnutls-cli -p smtp imap.gmail.com --starttls

this means the signal is sent to the outer sudo process. Obviously this does not work at all.

Is there any way to get sudo to exec the subprocess directly or to forward signals to its child?



A quick look at the man pages for sudo and sudoers reveal nothing obvious to solve this problem, but can I offer an alternative solution?

Wrap the original program in a script that will save the spawned pid# into a pidfile, which you can reference later. For example:


gnutls-cli $@ &
echo $! > /home/myuser/var/gnutls-cli.pid

Then, from sudo you can run:

sudo -u myuser /home/myuser/scripts/gnutls-cli-wrapper.sh -p smtp imap.gmail.com --starttls &

Then, later:

sudo -u myuser kill -ALRM $(cat /home/myuser/var/gnutls-cli.pid)

I hope you find this helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.