Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:17:34+00:00 2026-05-22T02:17:34+00:00

I’ve got a long running, daemonized Python process that uses subprocess to spawn new

  • 0

I’ve got a long running, daemonized Python process that uses subprocess to spawn new child processes when certain events occur. The long running process is started by a user with super user privileges. I need the child processes it spawns to run as a different user (e.g., “nobody”) while retaining the super user privileges for the parent process.

I’m currently using

su -m nobody -c <program to execute as a child>

but this seems heavyweight and doesn’t die very cleanly.

Is there a way to accomplish this programmatically instead of using su? I’m looking at the os.set*uid methods, but the doc in the Python std lib is quite sparse in that area.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:17:35+00:00Added an answer on May 22, 2026 at 2:17 am

    Since you mentioned a daemon, I can conclude that you are running on a Unix-like operating system. This matters, because how to do this depends on the kind operating system. This answer applies only to Unix, including Linux, and Mac OS X.

    1. Define a function that will set the gid and uid of the running process.
    2. Pass this function as the preexec_fn parameter to subprocess.Popen

    subprocess.Popen will use the fork/exec model to use your preexec_fn. That is equivalent to calling os.fork(), preexec_fn() (in the child process), and os.exec() (in the child process) in that order. Since os.setuid, os.setgid, and preexec_fn are all only supported on Unix, this solution is not portable to other kinds of operating systems.

    The following code is a script (Python 2.4+) that demonstrates how to do this:

    import os
    import pwd
    import subprocess
    import sys
    
    
    def main(my_args=None):
        if my_args is None: my_args = sys.argv[1:]
        user_name, cwd = my_args[:2]
        args = my_args[2:]
        pw_record = pwd.getpwnam(user_name)
        user_name      = pw_record.pw_name
        user_home_dir  = pw_record.pw_dir
        user_uid       = pw_record.pw_uid
        user_gid       = pw_record.pw_gid
        env = os.environ.copy()
        env[ 'HOME'     ]  = user_home_dir
        env[ 'LOGNAME'  ]  = user_name
        env[ 'PWD'      ]  = cwd
        env[ 'USER'     ]  = user_name
        report_ids('starting ' + str(args))
        process = subprocess.Popen(
            args, preexec_fn=demote(user_uid, user_gid), cwd=cwd, env=env
        )
        result = process.wait()
        report_ids('finished ' + str(args))
        print 'result', result
    
    
    def demote(user_uid, user_gid):
        def result():
            report_ids('starting demotion')
            os.setgid(user_gid)
            os.setuid(user_uid)
            report_ids('finished demotion')
        return result
    
    
    def report_ids(msg):
        print 'uid, gid = %d, %d; %s' % (os.getuid(), os.getgid(), msg)
    
    
    if __name__ == '__main__':
        main()
    

    You can invoke this script like this:

    Start as root…

    (hale)/tmp/demo$ sudo bash --norc
    (root)/tmp/demo$ ls -l
    total 8
    drwxr-xr-x  2 hale  wheel    68 May 17 16:26 inner
    -rw-r--r--  1 hale  staff  1836 May 17 15:25 test-child.py
    

    Become non-root in a child process…

    (root)/tmp/demo$ python test-child.py hale inner /bin/bash --norc
    uid, gid = 0, 0; starting ['/bin/bash', '--norc']
    uid, gid = 0, 0; starting demotion
    uid, gid = 501, 20; finished demotion
    (hale)/tmp/demo/inner$ pwd
    /tmp/demo/inner
    (hale)/tmp/demo/inner$ whoami
    hale
    

    When the child process exits, we go back to root in parent …

    (hale)/tmp/demo/inner$ exit
    exit
    uid, gid = 0, 0; finished ['/bin/bash', '--norc']
    result 0
    (root)/tmp/demo$ pwd
    /tmp/demo
    (root)/tmp/demo$ whoami
    root
    

    Note that having the parent process wait around for the child process to exit is for demonstration purposes only. I did this so that the parent and child could share a terminal. A daemon would have no terminal and would seldom wait around for a child process to exit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.