管理者権限がない場合は、GNOMEログアウト中にスクリプトをどのように実行しますか?

管理者権限がない場合は、GNOMEログアウト中にスクリプトをどのように実行しますか?

GNOMEセッションを終了する前にペンドライブをコンピュータに挿入することを忘れた場合は、自分に警告するスクリプトを実行したいと思います。

しかし、私が見つけたすべての解決策では十分ではありませんでした。

  • この同様の質問ファイルを編集する提案がありましたが、/etc/gdm/PostSession/Defaultそれを行う権限がありません。 (そしてファイルは現在私のコンピュータに空であるため、悪用する既存のフックはありません。)

  • 私も見つけました。この投稿回避策を提案してください。最初の回答によると、ログアウトせずにコンピュータをシャットダウンしても機能しません。

ベストアンサー1

Gnomeを使用するときは、上記のスクリプトを変更してからPythonスクリプトを使用できます。

gnome-session-quitGUIでログアウトを使用するときに発生するGnomeログアウト(例:)(またはgnome終了)が必要です。 AFAIKはコマンドsudo shutdown -h 0またはシャットダウンを介してプロセスをブロックできませんsudo poweroff。実行されると、shutdownすべてのプロセスにSIGTERMシグナルを送信して終了するまで数秒かかります(root以外のユーザーが編集できないいくつかのスクリプトを実行した後)。終了しない場合、SIGKILL はプロセスを強制終了します。

以下は、方法の段階的なプロセスですgnome_save_yourself。テストしてみましょう。

  1. 次のコードを次のように保存します~/Desktop/execute_script_on_shutdown.sh (fromhttp://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(または起動時に自動的に実行されるように設定)。

おすすめ記事