/*********************************************************************
* Author : Samson
* Date : 06/30/2014
* Test platform:
* Mint 15
* GNU bash, version 4.2.45
* *******************************************************************/
很多时候,在写了一个UI后,需要在执行的时候以超级用户的级别进行运行时,那么这就要在程序进行启动的时候进行使用超级用户级别进行运行程序,如有一个名称为systemtool的工具程序需要如些运行时,此工具的执行文件放在/usr/bin目录下,那么此文件的内容为:
/usr/bin/systemtool
#!/bin/sh
magic='--calling-python-from-/bin/sh--'
"""exec" python -E "$0" "$@" """#$magic"
import os
import sys
import commands
launcher = commands.getoutput("/usr/lib/linuxmint/common/mint-which-launcher.py")
command = "%s python /usr/share/systemtool/daemon/testpygtk.py" % (launcher)
os.system(command)
而/usr/lib/linuxmint/common/mint-which-launcher.py文件的内容又如下:
#!/usr/bin/python
import os
import gettext
gettext.install("mint-common", "/usr/share/linuxmint/locale")
if os.path.exists("/usr/bin/gksu"):
launcher = "gksu --message \"<b>" + _("Please enter your password") + "</b>\""
elif os.path.exists("/usr/bin/kdesudo"):
launcher = "kdesudo -i /usr/share/linuxmint/logo.png -d --comment \"<b>" + _("Please enter your password") + "</b>\""
print launcher
command的值为:gksu --message "<b>请输入您的密码</b>" python /usr/share/systemtool/daemon/testpygtk.py
而gksu命令是以su root的权限进行运行。