用户注销时的脚本执行(非root用户)


8

我在大学实验室里统一运行ubuntu 12.10,没有root权限。我需要在注销时运行脚本。这可能吗?

注意:这可能是此问题的重复,但是给出的答案是非常隐晦的,没有给出具体说明。


取决于是图形登录还是“命令行”登录
geirha,

这是图形登录。
geo909

3
我猜您可以编写一个运行所需的注销脚本,然后使用gnome-session-quit或类似的方法注销。
Adobe

@adobe嗯。。谢谢,在我看来,这是一个不错的解决方法。奇怪的是,没有特权的用户似乎没有一种直接的方法来处理这种情况。顺便说一句,我似乎没有能力投票,这可能是由于代表低。
geo909

1
相关信息,对于Gnome:如果没有管理员权限
吉尔(Gilles)'所以

Answers:


4

这是gnome_save_yourself方法的逐步过程。让我们做一个测试。

  1. 将以下代码另存为~/Desktop/execute_script_on_shutdown.sh (来自http://www.linuxquestions.org/questions/linux-desktop-74/gnome-run-script-on-logout-724453/#post3560301

    #!/usr/bin/env python
    
    #Author: Seamus Phelan
    
    #This program runs a custom command/script just before gnome shuts 
    #down.  This is done the same way that gedit does it (listening for 
    #the 'save-yourself' event).  This is different to placing scipts 
    #in /etc/rc#.d/ as the script will be run before gnome exits.
    #If the custom script/command fails with a non-zero return code, a 
    #popup dialog box will appear offering the chance to cancel logout
    #
    #Usage: 1 - change the command in the 'subprocess.call' in 
    #           function 'session_save_yourself' below to be what ever
    #           you want to run at logout.
    #       2 - Run this program at every gnome login (add via menu System 
    #           -> Preferences -> Session)
    # 
    #
    
    import sys
    import subprocess
    import datetime
    
    import gnome
    import gnome.ui
    import gtk
    
    
    class Namespace: pass
    ns = Namespace()
    ns.dialog = None
    
    
    def main():
        prog = gnome.init ("gnome_save_yourself", "1.0", gnome.libgnome_module_info_get(), sys.argv, [])
        client = gnome.ui.master_client()
        #set up call back for when 'logout'/'Shutdown' button pressed
        client.connect("save-yourself", session_save_yourself)
        client.connect("shutdown-cancelled", shutdown_cancelled)
    
    
    def session_save_yourself( *args):
            #Lets try to unmount all truecrypt volumes
    
    
        #execute shutdowwn script
        #########################################################################################
        retcode = subprocess.call("bash /home/totti/Desktop/shutdown_script.sh", shell=True)
        ##########################################################################################
        if retcode != 0:
            #command failed  
            show_error_dialog()
        return True
    
    def shutdown_cancelled( *args):
        if ns.dialog != None:
            ns.dialog.destroy()
        return True
    
    
    def show_error_dialog():
        ns.dialog = gtk.Dialog("There was a problem running your pre-shutdown script",
                               None,
                               gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                               ("There was a problem running your pre-shutdown script - continue logout", gtk.RESPONSE_ACCEPT))
        if ns.test_mode == True:
            response = ns.dialog.run()
            ns.dialog.destroy()
        else:
            #when in shutdown mode gnome will only allow you to open a window using master_client().save_any_dialog()
            #It also adds the 'Cancel logout' button
            gnome.ui.master_client().save_any_dialog(ns.dialog)
    
    
    
    #Find out if we are in test mode???
    if len(sys.argv) >=2 and sys.argv[1] == "test":
        ns.test_mode = True
    else:
        ns.test_mode = False
    
    if ns.test_mode == True:
        main()
        session_save_yourself()
    else:
        main()
        gtk.main() 
  2. 使它可执行:

    chmod +x ~/Desktop/execute_script_on_shutdown.sh
  3. 将以下内容另存为 ~/Desktop/shutdown_script.sh

    #!/usr/bin/bash
    touch ~/Desktop/AAAAAAAAAAAAAAAAAAAAAAAAAAA  
  4. 执行主脚本

    bash ~/Desktop/execute_script_on_shutdown.sh

现在您感觉脚本正在等待某事

  1. 注销或关闭操作系统(Ubuntu)
  2. 登录
  3. 检查AAAAAAAAAAAAAAAAAAAAAAAAAAA在桌面上命名的文件。

    ls -l ~/Desktop/AAAAAAAAAAAAAAAAAAAAAAAAAAA

如果看到文件,则一切正常。现在,您可以编辑shutdown_script.sh以满足您的需要。还请记住execute_script_on_shutdown.sh在登录时执行(或使其在启动时自动可执行)。



不幸的是,我将离开校园一段时间,并且无法访问gnome桌面。如果这是测试和工程,我能接受的答案,但..
geo909

在Ubuntu 12.10上测试
totti 2014年


1
已在Ubuntu 12.10上成功测试,并具有统一性
totti 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.