從掛起恢復時從腳本運行程序

從掛起恢復時從腳本運行程序

我嘗試使用位於包含以下內容的/lib/systemd/system-sleep/檔案中的腳本在從睡眠狀態恢復系統時執行程式 RealTimeSync:RealTimeSync_kill_suspend.sh

#!/bin/sh

case $1 in
        pre)
                echo "$(date) - $1: Killing RealTimeSync" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                kill -9 `ps -aux | pgrep RealTimeSync`
                exit
                ;;
        post)
                echo "$(date) - $1: Invoking RealTimeSync resume script" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                sh /home/bart/Applications/FreeFileSync/RealTimeSync_resume.sh
;;
esac

RealTimeSync_resume.sh我知道它執行具有以下內容的子腳本:

#!/bin/sh

echo "$(date) - Running RealTimeSync" >> /home/bart/Applications/FreeFileSync/suspend_resume.log

/home/bart/Applications/FreeFileSync/RealTimeSync /home/bart/Documents/Documents_backup.ffs_real &

echo "$(date) - RealTimeSync should be running" >> /home/bart/Applications/FreeFileSync/suspend_resume.log

exit

因為它將腳本echo中兩個語句之後的行RealTimeSync_kill_suspend.sh和子腳本echo中兩個語句之後的行RealTimeSync_resume.sh放入日誌檔案中suspend_resume.log

czw, 5 sie 2021, 16:55:50 CEST - pre: Killing RealTimeSync
czw, 5 sie 2021, 16:55:58 CEST - post: Invoking RealTimeSync resume script
czw, 5 sie 2021, 16:55:58 CEST - Running RealTimeSync
czw, 5 sie 2021, 16:56:28 CEST - RealTimeSync should be running

但是當我尋找過程時,ps -aux | grep RealTimeSync它沒有顯示任何正確的匹配,只是:

bart       31262  0.0  0.0  12252  2612 pts/0    S+   17:38   0:00 grep --color=auto RealTimeSync

當我運行子腳本時,sh /home/bart/Applications/FreeFileSync/RealTimeSync_resume.sh我得到正確的進程運行ps -aux | grep RealTimeSync

bart       31066  0.0  0.0    212    68 pts/0    S    17:37   0:00 /home/bart/Applications/FreeFileSync/RealTimeSync /home/bart/Documents/Documents_backup.ffs_real
bart       31071  0.3  0.1 442428 41260 pts/0    Sl   17:37   0:00 /home/bart/Applications/FreeFileSync/Bin/RealTimeSync_x86_64 /home/bart/Documents/Documents_backup.ffs_real
bart       31262  0.0  0.0  12252  2612 pts/0    S+   17:38   0:00 grep --color=auto RealTimeSync

所有提到的文件都有-rwxr-xr-x權限。

在論壇中搜尋後,我發現 RealTimeSync 需要一些在登入時啟動的服務,但該服務不可用,例如,在運行啟動 synclient 的腳本時遇到問題的人需要 X 伺服器連接到:

declare -x DISPLAY=":0.0"
declare -x XAUTHORITY="/home/<your user>/.Xauthority"
synclient VertEdgeScroll=1 VertTwoFingerScroll=1 HorizTwoFingerScroll=1 HorizEdgeScroll=1

來自這個論壇主題:https://ubuntuforums.org/showthread.php?t=2380045

我將不勝感激任何幫助。

編輯1

我發現“FreeFileSync 和 ReadTimeSync 需要訪問圖形 X11 顯示,因此它們無法通過系統模式運行。在用戶模式下,systemd 了解用戶圖形會話並使用它。”所以有兩種潛在的解決方案:

上面提到的任何一種,硬編碼DISPLAYXAUTHORITY,都不鼓勵,因為 DISPLAY 值可能因會話而異。

或作為用戶服務運行,而不是系統服務,因為顯示是為用戶初始化的。

我在這裡發現了這一點,並對將 FreeFileSync 作為系統服務運行進行故障排除:https://unix.stackexchange.com/questions/529115/system-service-error-unable-to-initialize-gtk-is-display-set-properly

不幸的是,我在實施這些解決方案時遇到問題,如果有任何幫助,我將不勝感激。

編輯2

好吧,我成功了!我現在從主腳本運行 RealTimeSync,儘管我猜這沒有任何區別,重要的是初始化“DISPLAY”,如下所示:

#!/bin/sh

case $1 in
        pre)
                echo "$(date) - $1: Killing RealTimeSync" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                kill -9 `ps -aux | pgrep RealTimeSync`
                exit
                ;;
        post)
                echo "$(date) - Invoking RealTimeSync resume script" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                env DISPLAY=:1 sudo -u bart /home/bart/Applications/FreeFileSync/RealTimeSync /home/bart/Documents/Documents_backup.ffs_real
;;
esac

並將該腳本的所有者設定為 root:chown root:root <script_name>

解決方案來自這裡:以登入使用者(非 root)身份恢復後啟動腳本

據我了解,這只是一種解決方法,而不是正確的解決方案,因為硬編碼DISPLAY可能會帶來問題,但暫時有效。如果有人知道如何以使用者而不是 root 身分正確運行它,我將不勝感激任何提示。

相關內容