Linux小技巧暨問題集


加入開機啟動無反應,手動執行有動作

原本寫了幾隻python,打算開機後用一隻shell script自動執行,script內容如下

#!/bin/bash

fbee_solution_dir='/home/firstuser/dev/fbee_solution'
log_path='/home/firstuser/dev/fbee_solution/tool/check_server_process.sh.log'

while :
do
    socket_server_pid=$(ps aux |grep 'socket_server.py' |grep -v 'grep' |tr -s ' ' ' ' |cut -d' ' -f2)
    report_server_pid=$(ps aux |grep 'report_server.py' |grep -v 'grep' |tr -s ' ' ' ' |cut -d' ' -f2)
    now_datetime=$(date +'%Y%m%d-%H%M%S')

    if [ -z ${socket_server_pid} ];then
        cd ${fbee_solution_dir}
        echo "${now_datetime}: socket_server stop" >> ${log_path}
        python3 socket_server.py >> /dev/null 2>&1 &
    fi

    if [ -z ${report_server_pid} ];then
        cd ${fbee_solution_dir}
        echo "${now_datetime}: report_server stop" >> ${log_path}
        python3 report_server.py >> ${log_path}
    fi
done

加入rc.local

cd /home/firstuser/dev/fbee_solution/tool/ && ./check_server_process.sh

重開後,發現socket_server.py有啟動,report_server.py沒動作,kill掉check_server_process.sh後,手動執行check_server_process.sh卻正常,兩隻都有動作
到終端機前面看,發現畫面顯示找不到pymongo模組的訊息,後來想了一下,判斷問題可能出在當初安裝pymongo時,只安裝在firstuser下,而開機啟動是用root執行,因此找不到pymongo模組
解決方式
參考 https://zh-hant.hotbak.net/key/linux使用某非root用戶執行開機啟.html 進行如下修改

$sudo vi /etc/rc.local中
將
cd /home/firstuser/dev/fbee_solution/tool/ && ./check_server_process.sh
改為
su - firstuser -c "cd /home/firstuser/dev/fbee_solution/tool/ && ./check_server_process.sh"
#linux #ubuntu






你可能感興趣的文章

[練習] CSS: Loaders

[練習] CSS: Loaders

[迷你電商專案 2.0] 重新開始的第一周 - 制定計畫

[迷你電商專案 2.0] 重新開始的第一周 - 制定計畫

How to Create a Load Balancer with an HTTPS Listener

How to Create a Load Balancer with an HTTPS Listener






留言討論