acheung456
acheung456

Reputation: 13

Using File.chown() in ruby as root to be owned by user

I'm looking to simply open and write a new file, then have it owned by another user (while running as root). The file creation works alright but the file still ends up being owned by root. I'm using:

doc="text"
uid=Etc.getpwnam("#{$user}").uid

File.open("#{$file}", 'w') {|f| 
            f.write(doc)
            f.chown($uid,$uid)
    }

Any help is greatly appreciated :)

Upvotes: 1

Views: 1014

Answers (1)

Dan Grahn
Dan Grahn

Reputation: 9424

Only a process with superuser privileges may change the owner of a file. [RubyDocs]

Try running ruby with sudo.

EDIT

Second argument should be group ID. Not user ID.

EDIT 2

Turned out to be the globals. My heart has never been happier. Please see comments.

Upvotes: 1

Related Questions