hyperlink
hyperlink

Reputation: 147

How can I set the umask of a process I am spawning?

I have a service which is a Python REST API. This service executes some scripts internally using,

subprocess.Popen(cmd, shell=False, preexec_fn=os.setpgrp, env=self.env, universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)

Now I want to set the umask of the process that this spawns. I tried setting the umask in the init script that runs this service. But it looks like it doesn't get inherited by the sub-process being spawned by the service. Trying to get this done on RHEL if that makes a difference. Thanks !

Upvotes: 3

Views: 2383

Answers (1)

Norman
Norman

Reputation: 1975

Since the preexec_fn argument seems to work for you, you can use it to call os.umask() as well:

def initchildproc():
    os.setpgrp()
    os.umask(400)

subprocess.Popen(cmd, shell=False, preexec_fn=initchildproc, ...)

Upvotes: 5

Related Questions