執行註銷腳本

執行註銷腳本

我一直在自學如何編寫登入腳本,但我很難找到有關編寫登出腳本的資訊。我希望能夠在使用者登出 X 會話時記錄日誌條目。

這是我想在登出時執行的操作:

#!/bin/bash

# This script is intended to record when a user logs out of a TTY shell or the X session.
# This will record the results to a log file: logoutScript.log

# Variables used in this script.
dDate=$(date +%Y/%m/%d)
dTime=$(date +%l:%M%P)
logDir=/opt/scripts/logs
logFile=$logDir/logoutUser.log

# Begin operation of script.
echo "$dDate, $dTime - User '$USER' has logged out of the system." >> $logFile

# End of file
exit 0

我知道如果我在登入腳本上使用以下內容,它就會按照我想要的方式運作。那麼如何對 LOGOUT 腳本執行相反的操作呢?

# Detect if user is logged into TTY shell or X session, then execute corresponding login script.
if xhost >& /dev/null ; then
    gnome-terminal -e "bash -c \"cd /opt/scripts && ./loginScripts.sh && cd $HOME \""
else 
    bash -c "cd /opt/scripts && ./loginScripts.sh && cd $HOME"
fi

我真的很感激任何幫助。謝謝。

答案1

Ctrl透過同時按下+ Alt+T或在破折號中找到它來開啟終端機(或也稱為命令列) 。

運行此命令sudo gedit /etc/lightdm/lightdm.conf。這將開啟文字編輯器,其中包含位於資料夾中的 lightdm.conf 檔案/etc/lightdm

在文件中你應該有一行[SeatDefaults].在此行下方,輸入session-cleanup-script=/path/to/your-script.sh,其中/path/to/your-script.sh是要執行的腳本的實際位址。

注意:透過執行以下命令確保您的腳本設定為可執行:

sudo chmod +x /path/to/script.sh

儲存並退出。

注意:從 14.04 版本開始,設定檔不存在,必須手動建立或使用以下命令從範例檔案複製:

sudo sh -c 'zcat /usr/share/doc/lightdm/lightdm.conf.gz > /etc/lightdm/lightdm.conf'

如果您只想在終端上註銷一個腳本,只需將其放入~/.bash_logout.

相關內容