您可以做的另一件事是,如果脚本不是以root身份执行的,则脚本会自动调用sudo:
import osimport syseuid = os.geteuid()if euid != 0: print "script not started as root. Running sudo.." args = ['sudo', sys.executable] + sys.argv + [os.environ] # the next line replaces the currently-running process with the sudo os.execlpe('sudo', *args)print 'Running. Your euid is', euid输出:
script not started as root. Running sudo..[sudo] password for bob:Running. Your euid is 0
使用
sudo -k进行测试,以清除所以下一次运行该脚本时它会再次需要密码,你的sudo时间戳。



