Running a Python script with root permissions
FeedbackBy Preston Hunt, 07 November 2013
Previously, I wrote a script that automatically escalated to root permissions if needed.

This works fine for shell scripts, but recently I needed a way to do this for Python scripts. The trouble is that the shebang #! syntax only allows limited command-line argument passing.

This following recipe is not pretty, but it seems to work!

#!/usr/bin/env bash

if (( UID != 0 )); then
	echo re-running as root
	exec sudo -E "$0" "$@"

python2 -u <(cat <<"EOF" print "hello, world!" EOF ) "$@"

blog comments powered by Disqus