1

How to use pkill as su on a Mac?

When I run su pkill -9 "RFBEventHelperd" in the terminal I get su: Sorry (but works as sudo). I am looking for a way to kill this process from inside a script that runs as su.

My application

I use SleepWatcher in MacOS to open Screen Sharing apps on wake up. After launching Screen Sharing app I want to kill the process RFBEventHelperd as this gives back Command+Tab to Mac instead of being bound to OS running in Screen Sharing.

SleepWatcher runs wake script as su:

$ su - $user -c "$home/.wakeup"

Question

How can I put the pkill -9 "RFBEventHelperd" into my script, and have it kill a process that's running as another user.

  • Can you please elaborate on this a bit? It's confusing what you want to do here. You want to run su - $user -c "pkill -9 RFBEventHelperd' inside of another script that you already su -c .. in when you ran it? – slm Jul 17 '18 at 1:23
  • I wouldn't use su inside the script that is already executed as su. I am testing it in the terminal. To make it clear, first 2 lines of my question talks about using pkill in the terminal as su followed by explanation of my application. – Jsp Jul 17 '18 at 1:32
  • Yeah I'm trying to make heads/tails of your question. It's still confusing to me. Perhaps you could add the contents of ps -eaf to show the process you're trying to kill? Sorry I'm trying to understand it but this lacks enough detail to help. – slm Jul 17 '18 at 1:33
  • su pkill -9 "RFBEventHelperd" results in su: Sorry, do you know how to use pkill as su? – Jsp Jul 17 '18 at 1:35
  • I do, but you do too, you're showing su - $user -c "<cmd>". – slm Jul 17 '18 at 1:36
0

The form of su that you're looking for is as follows:

$ su -c pkill -9 "RFBEventHelperd" <user>

On OSX this form may not work. In those situations you'll likely have to yield to using sudo instead:

$ sudo -u <user> <cmd>

For this to run passwordless you'll have to create an entry for this in your /etc/sudoers file and utilize the NOPASSWD feature for an explicit command that your use running the original script has access to execute without being challenged for the password.

Using sudo

To set up a rule in /etc/sudoers file to allow this user access to do the pkill command one could add this to /etc/sudoers:

%admin ALL=(ALL) NOPASSWD: ALL

And with this the shell script can then run this command without any password:

$ sudo -u root /usr/bin/pkill -9 "RFBEventHelperd"

NOTE: When dealing with /etc/sudoers edits you can use visudo like so:

$ sudo visudo

References

  • Thanks @slm. I added %admin ALL=(ALL) NOPASSWD: ALL to /etc/sudoers file and added sudo -u root /usr/bin/pkill -9 "RFBEventHelperd" to the script and it is working fine. Is there any security drawback to this approach? – Jsp Jul 17 '18 at 16:54
  • @Jsp - I added this to the answer. This seems like an appropriate way to address this. You've wisely narrowed the command to just 1 thing that a user in the %admin group can run, nothing bad that I can see with this. – slm Jul 17 '18 at 17:02

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.