ユーザーがセッションを開始した後、rootコマンドを実行します。

ユーザーがセッションを開始した後、rootコマンドを実行します。

ユーザーがセッションを開始したときにrootコマンドを実行する方法を理解しようとしています。

このファイルにコマンドを追加すると、/etc/rc.local起動後にコマンドを実行する必要がありますが、コマンドが実行されないか、システムがコマンドを実行する準備ができていないようです。 (このコマンドはうまく動作します)

たぶん、例えば私がやろうとしていることを明確にすることができます。ほとんどすべてのデスクトップマネージャの設定ウィンドウには「セッションと起動」というオプションがあり、「アプリケーションの自動起動」セクションの下に現在のユーザーがログインしたときに実行されるコマンドを追加できます。

これをしたいがroot権限を必要とするコマンドを使用します。

ベストアンサー1

解決策を見つけ、ユーザーを検索するスクリプトを作成しました。

これは私の/etc/rc.localスクリプトです。

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.


/usr/bin/detect_login 
exit 0

detector_login スクリプトは次のとおりです。

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-


import os, time

Buffer_list=[]
while True:
    users=os.popen('''who | cut -d' ' -f1 | sort | uniq''')
    users=users.read()
    Current_List=users.split('\n')
    Current_List=filter(None,Current_List)
    if Current_List:
        if Current_List != Buffer_list:

            if len(Current_List) > len(Buffer_list):

                #HERE YOU ADD THE COMMANDS, inside the triple quotes.
                # add each command in a new line

                # i let you an example for turning the brightness down..
                os.system('''/usr/bin/xdotool key XF86MonBrightnessDown''') 


            Buffer_list=Current_List

    time.sleep(0.5)

エラーが発生するとrc.localが停止するため、rootでスクリプトを一度実行して正常に動作することを確認することをお勧めします。 (愚かな間違いは、例えば空白のインデントなどになる可能性があります。これは、stackexchangeフォーラムからPythonスクリプトをコピーするときに頻繁に発生します)

おすすめ記事