I have a python script which is performing some nagios
configuration. The script is running as a user which has full sudo rights (the user can run any command with sudo, without password prompt). The final step in the configuration is this:
open(NAGIOS_COMMAND_FILE, 'a').write(cmdline)
The NAGIOS_COMMAND_FILE
is only writable by root, so this command should be run by root. I can think of two ways of achieving this (both unsatisfactory):
- Run the whole script as root. I do not like doing this, since any error in my script will be executed with full root rights.
- Put the
open(NAGIOS_COMMAND_FILE, 'a').write(cmdline)
command in a separate script, and use thesubprocess
library to call that script, with sudo. I do not like creating an extra script just to run a single command.
I suppose there is no way of changing the running user just for a single command, in my current script, or am I wrong?