如何在重新啟動服務之前更改給定目錄的所有權

如何在重新啟動服務之前更改給定目錄的所有權

我正在嘗試編寫一個腳本並將其放入 cron 作業中。

該腳本將每5 分鐘檢查一次進程是否正在運行,如果沒有運行,則必須啟動該進程,但我的問題是,當進程啟動時,它由root 擁有,並且會更改特定目錄中的一些文件,因此,我想讓腳本啟動服務,然後等待 10 秒並停止服務,之後它將轉到特定目錄並更改該目錄中所有檔案的所有權,然後腳本將再次啟動服務。

我希望我的解釋有意義。

這是我到目前為止想到的。該腳本用於啟動和停止服務,但當服務停止時會逾時,並且不會更改給定目錄中的任何所有權

#!/bin/bash
exec_start="sudo systemctl start new-service"
exec_stop="sudo systemctl stop new-service"
check1=/root/testing/
username="user1"
process="new-service"

changing_ownership () {
cd $check1 && /usr/bin/chown $username:$username *
}

startAndStop(){
/usr/bin/sudo su - user1 && $exec_start &
 sleep 10 && $exec_stop &
 sleep 2 && exit
}

startAgain(){
/usr/bin/sudo su - user1 && $exec_start &
sleep 5 && exit
}

if ps ax | grep -v grep | grep $process > /dev/null
then
 echo "running"
 exit

else
 echo "Not running...Going to start the service"
 startAndStop
 changing_ownership
 startAgain
fi

exit

這是/root/testing/的內容

[root@server2 ~]# cd -
/root/testing
[root@server2 testing]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 24 21:34 file1
-rw-r--r--. 1 root root 0 Sep 24 21:34 file2
-rw-r--r--. 1 root root 0 Sep 24 21:34 file3
-rw-r--r--. 1 root root 0 Sep 24 21:39 file8888

謝謝

答案1

我看到幾個問題。

我做了一個簡單的測試腳本:

#!/bin/bash
startAndStop(){
        echo "Start 1" &
        echo "Sleep 2" && echo "Stop 3"
        echo "Sleep 4" && exit
}
startAndStop
echo "Rest of Script"

這模擬了程式的 startAndStop 函數。

這是輸出:

$ bash test.sh
Sleep 2
Stop 3
Sleep 4
Start 1

關於這個函數有兩點:

  1. 事情執行無序。單一&背景啟動任務。您確定要這樣做嗎?您不想等待過程完成後再開始停止它嗎?我知道您放入了sleeps,但我認為等待它退出更好。如果命令沒有及時完成,事情可能會變得非常奇怪。

  2. 請注意它是如何沒有列印出來的Rest of Script。該exit命令不會退出該函數,而是退出整個腳本。

你有/usr/bin/sudo su - user1 && $exec_start。我不認為那是你想做的事。你設定exec_start="sudo systemctl start new-service"。因此,您最終要做的就是切換到 user1,然後再提升到 root 權限來啟動服務。新腳本,在我的機器上。$(pwd)將列印當前目錄。

#!/bin/bash
sudo su - user1 && /usr/bin/sudo echo $(pwd)

讓我們執行它:

[root]$ bash test.sh
[user1]$

我有殼了!這&& 等待完成上一個指令的 - 即sudo su -,它提供了一個 root shell。echo直到我離開 shell 後該指令才執行:

[user1]$ exit
logout
/root
[root]$

即使如此,它還是給出了/root目錄,而不是/home/user1.這絕對不是你想要的。將此腳本新增至 crontab 中運行如何作為用戶?這將是 root 的 crontab 行:

 */5      *       * * *   user1    /home/user1/new-service-watch.sh

這樣,您就不必搞亂雙重切換用戶。該腳本將由cronas啟動user1,然後您只需sudo運行即可systemctl,並將其他所有內容保留為該用戶。

這是一個快速的想法。請不要照原樣使用!

編輯:更改腳本以根據請求以 user1 身份啟動服務。$sudo_cmdexec_start和定義中刪除exec_stop

*edit2:sudo如果檔案歸 root 所有,則應使用 chowning。另外,應該停止服務,sudo以防 root 啟動該進程。

#!/bin/bash
sudo_cmd="/usr/bin/sudo" # these are just my paths, change as needed
chown_cmd="$sudo_cmd /usr/bin/chown" # added sudo
systemctl_cmd="/bin/systemctl"
date_cmd="/bin/date"
# if define process before exec_start & exec_stop, 
# you can use it in your exec_start & exec_stop definitions
process="new-service"
exec_start="$systemctl_cmd start $process" # don't sudo so it runs as user1
exec_stop="$sudo_cmd $systemctl_cmd stop $process" # added sudo
check1="/root/testing"
username="user1"
log_file="/var/log/new-service-watch.log" # why not log things? :)

# putting the entirety of the execution in 
# braces lets you pipe it somewhere easily
{ # start_brace
changeOwnership()
{
    echo "$($date_cmd) Changing ownership"
    # there is no need to change directory
    # because we will use '-R' to be recursive
    $chown_cmd -R $username:$username $check1
}

startService()
{
    echo "$($date_cmd) Starting $process"
    $exec_start
    sleep 5
}

stopService()
{
    echo "$($date_cmd) Stopping $process"
    $exec_stop
    sleep 5
}

if ps ax | grep -v grep | grep $process > /dev/null
then
    echo "$($date_cmd) Running...No action needed."
else
    echo "$($date_cmd) Not running...Going to start the service."
    startService
    stopService
    changeOwnership
    startService
fi
} >> "$log_file" # end_brace

相關內容