Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 359
hyperlink Avatar asked Dec 11 '25 03:12

hyperlink


1 Answers

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, ...)
like image 95
Norman Avatar answered Dec 14 '25 00:12

Norman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!