# Run system command as user

**URL:** <https://www.ruby-forum.com/t/run-system-command-as-user/121804>\
**Category:** Ruby\
**Created:** [December 1, 2007, 3:01am UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804 "2007-12-01T03:01:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Trey](https://www.ruby-forum.com/u/Trey)\
**Post date:** [December 1, 2007, 3:01am UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804/1 "2007-12-01T03:01:49Z")

</div>

I’m in need of running a system command as another user than the user  
that is executing the ruby script. Is this possible? Can I execute  
su (and somehow supply the correct password), then run the command?

---

<div class="post-metadata">

**Author:** ![Trey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Trey](https://www.ruby-forum.com/u/Trey)\
**Post date:** [December 1, 2007, 9:45am UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804/2 "2007-12-01T09:45:37Z")

</div>

On Nov 30, 7:56 pm, Trey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m in need of running a system command as another user than the user  
> that is executing the ruby script. Is this possible? Can I execute  
> su (and somehow supply the correct password), then run the command?

Here is a naive version using the built-in pty extension (\*nix only,  
but since you mention su, I assume that’s not a problem)…

require “pty”  
require “expect”

cmd = “sleep 1; sudo -u someuser ls 2\>&1”  
PTY.spawn(cmd) { | stdin, stdout, pid |  
stdin.expect(/Password:|/) { | result, pass |  
stdout.write(“secret\_password\n”) if pass  
}  
puts stdin.read  
}

Regardsm  
Jordan

---

<div class="post-metadata">

**Author:** ![Trey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Trey](https://www.ruby-forum.com/u/Trey)\
**Post date:** [December 1, 2007, 9:56am UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804/3 "2007-12-01T09:56:09Z")

</div>

On Dec 1, 2:41 am, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require “expect”  
> Regardsm  
> Jordan

Oops…

> ```
> stdout.write("secret_password\n") if pass
> 
> ```

```
stdout.write("secret_password\n") if pass == "Password:"

```

---

<div class="post-metadata">

**Author:** ![Trey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Trey](https://www.ruby-forum.com/u/Trey)\
**Post date:** [December 1, 2007, 10:00am UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804/4 "2007-12-01T10:00:49Z")

</div>

On Nov 30, 7:56 pm, Trey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m in need of running a system command as another user than the user  
> that is executing the ruby script. Is this possible? Can I execute  
> su (and somehow supply the correct password), then run the command?

Depending on how much control you have over the system and what’s  
available, you might be able to configure sudo to allow your script to  
run the command as a different user without a password. If it’s  
available and you’re able to configure sudo on the system (or have  
someone do it for you), check the sudoers man page for info on the  
NOPASSWD tag.

---

<div class="post-metadata">

**Author:** ![Trey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Trey](https://www.ruby-forum.com/u/Trey)\
**Post date:** [December 1, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804/5 "2007-12-01T17:50:35Z")

</div>

Sweet. It works. Thanks Jordan

---

<div class="post-metadata">

**Author:** ![Trey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Trey](https://www.ruby-forum.com/u/Trey)\
**Post date:** [December 1, 2007, 10:20am UTC](https://www.ruby-forum.com/t/run-system-command-as-user/121804/6 "2007-12-01T10:20:42Z")

</div>

On Dec 1, 2:52 am, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Here is a naive version using the built-in pty extension (\*nix only,  
> > puts stdin.read  
> > stdout.write(“secret\_password\n”) if pass == “Password:”  
> > Good grief. Third time’s a charm (maybe)…

require “pty”  
require “expect”

PTY.spawn(“sleep 1; sudo -u root ls 2\>&1”) { | stdin, stdout, pid |  
begin  
stdin.expect(“Password:”) {  
stdout.write(“secret\_password\n”)  
puts stdin.read.lstrip  
}  
rescue Errno::EIO  
# don’t care  
end  
}

Sorry about that! Was confusing live code with the version I meant to  
post (twice!). I was trying to do fancy stuff and detect when the  
password was catched already by sudo, but never got that working.

Regards,  
Jordan
