Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using newgrp in scripts?

Status
Not open for further replies.

xhonzi

Programmer
Jul 29, 2003
196
US
Okay,
My first attempts at this have been miserable. Anyone know of a way to use newgrp in a script so that the group that the script operates under is the one specified? Anyone know of any creative ways around this that don't include manually typing newgrp each time I run this particular script?

Thanks,
Xhonzi
 
I don't know which UNIX you have, but on AIX it is not possible to use newgrp or setgroups inside a script. Says so on the man page...

You might want to have a look at sudo, or else write a c-program that sets its own user and primary group (must have setuid and setgid bits on the permissions of the executable: chmod ug+s) and then runs your script - but that is exactly what sudo can do for you. You may have to create a user account that has the same uid and groups, but another primary group ...

I guess with expect, you could script it because then you are providing input to a "real" shell prompt on a pseudo-tty and you can throw any valid command at that prompt. But it's a bit harder to script...

HTH,

p5wizard
 
Why do you want the script to run under that group ID? Perhaps there is another way to achieve the same result...

Annihilannic.
 
Okay, I'll play that game.

These scripts create a lot of log files that I want to be owned by the "project" group. Other facets of this user (the one running the script) dictate that he must primarily be part of the "admin" group. I thought the easiest way to ensure that all files created by the script are owned by the project group would be to use newgrp at the beginning of the script, but we all know that doesn't work. I know I could use chgrp on the files after creation, but that's going to be A LOT of code to add in. We're talking hundreds of lines, here. I don't know much about this set GID business; that sounds like it could work.

Does that help?

Xhonzi
 
Well, yes setUID/setGID works, but I guess then your app won't, as you clearly state in your last post that the user needs to have a specific primary group...

If you alter the primary group of a process either with newgrp or with sudo or the like, I guess your app would object and not start...

HTH,

p5wizard
 
If you change the group ownership of the directory in which these files are created to the desired group and then set the sgid bit (i.e. chmod g+s dirname) on that directory, any files that are created in that directory will be owned by that group. If the script sets umask 002 the files created would also be group writeable.

Annihilannic.
 
Thanks.

For clarification, the user's primary group needs to be admin most of the time, but in the context of the script it doesn't.

Xhonzi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top