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

  • SEARCH
  • Home
  • 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 1013521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:06:50+00:00 2026-05-16T10:06:50+00:00

I have a bash backup script run as root (cron) that delegates certain tasks

  • 0

I have a bash backup script run as root (cron) that delegates certain tasks to other specific bash scripts owned by different users. (simplified example, principle is, some things have to be done as root, different tasks are delegated to users with the appropriate environment (oracle, amazon, …)

mkdir -p /tmp/backup$NAME
su - oracle -c "~/.backups/export-test.sh"
tar cf /tmp/backup/$NOW.tar /tmp/backup$NAME
su - amazon upload_to_amazon.sh /tmp/backup/$NOW.tar

This script itself does then some tasks as user oracle:

mkdir -p $TMP_LOCATION
cd ~/.backups
exp $TMP_LOCATION/$NAME-$NOW

When I try to mimic this behaviour in python I came up with the following (started from cron as root)

name = "oracle"

# part run as root
os.makedirs(tmp_backup + name)


os.setegid(pwd.getpwnam(name)[3])
os.seteuid(pwd.getpwnam(name)[2])

# part run as oracle
os.makedirs(tmp_location)
os.chdir(os.path.expanduser("~{user}/.backups".format(user=name)))
subprocess.check_call(["exp",
                       "os.path.join(tmp_location, name+'-'+now)"
                      ])

In bash when using su -, a real new shell is invoked and all environment variables of that user are set.
How can I improve this for my python script? Is there a standard recipe I can follow? I’m thinking of environment variables, umask, …

the environment is Solaris if that might matter.

  • 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-16T10:06:51+00:00Added an answer on May 16, 2026 at 10:06 am

    all environment variables of that user are set

    Usually because a shell runs a .profile file when it starts up.

    You have several choices.

    1. Create a proper subprocess with subprocess.Popen to execute the shell .profile — same as su -.

    2. Carefully locate the environment variable settings and mimic them in Python. The issue is that a .profile can do all kinds of crazy things, making it a potential problem to determine the exact effects of the .profile.

    3. Or you can extract the relevant environment variables to make the accessible to both the shell environment and your Python programs.

    First. Read the .profile for each user to be clear on what environment variables it sets (different from things like aliases or other craziness that doesn’t apply to your Python script). Some of these environment variables are relevant to the scripts you’re running. Some aren’t relevant.

    Second. Split the “relevant” environment variables into a tidy env_backups.sh script or env_uploads.sh script.

    Once you have those environment variable scripts, update your .profile files to replace the environment variables settings with source env_backup.sh or source env_uploads.sh.

    Third. Source the relevant env_this and env_that scripts before running the Python program. Now your Python environment shares the variables with your shell environment and you only maintain them in one place.

    my_script.sh

    source ~oracle/env_backup.sh
    source ~amazon/env_uploads.sh
    python my_script.py
    

    That seems best to me. (Since that’s how we do it.)

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

Sidebar

Related Questions

I have a bash script, that I run like this via the command line:
I have a share backup bash script that works as is, but it uses
I have a bash script (supports linux/unix), that installs an application. Instead of executing
I have a Bash script on server A that finds the logged in SSH
I have a bash script set that executes in a folder containing a number
I have a bash script that has set -x in it. Is it possible
I have a bash script which need to execute some php scripts and to
I have a bash script that does ssh to a remote machine and executes
I have a bash script that cuts out a section of a logfile between
I have a bash script that sources contents from another file. The contents of

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.