mikip
mikip

Reputation: 23

Executing a python script from inittab not as root

I have a python script which I would like to launch from inittab, shown below

s1:respawn:/home/a_user/app/script.py

I believe initab executes as root, so the a_user's envrinment is not available

The script needs to know "a_user" home directory for ini file settings and log file storage. I would like to avoid hard coding these paths in my script. Is it possible to execute this script as a_user and not a root? If this is possible would a_user HOME environment variable be available?

Regards

Upvotes: 1

Views: 1479

Answers (3)

Geremia
Geremia

Reputation: 5636

You can do something like:

ig:34:respawn:sudo -u <USER> bash -c 'cd <SCRIPT_WORKING_DIR> && ./PYTHON_SCRIPT.py'

Upvotes: 0

damir
damir

Reputation: 1978

you could copy python binary to python-suid, chown it to user you want to run scripts as and chown u+s python-suid

then in script #!/usr/bin/python-suid

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799044

Use runuser (or the distro's equvalent) to run it as a different user. runuser does change $HOME, but other similar commands may not.

Upvotes: 1

Related Questions