[英]Run Python program on bootup/reboot, on separate partition in OpenWrt Linux
在 OpenWrt Linux 上,我將啟動腳本放在 /etc/init.d 文件夾中,並啟用它。 腳本很好,如下所示:
#!/bin/sh /etc/rc.common
# Automatically place an "S91canpy" symlink in /etc/rc.d/ once we enable it
# This means it will start right after /etc/rc.d/90openvpn
START=91
# This is what will run when this service starts
start() {
# Run the process in the background (&), and direct errors to a log file
sh /user/start_everything.sh >/var/log/init_canpy.log 2>&1 &
}
# This is what will run when the service stops
stop() {
echo "no stop function set up yet"
}
# This is what will run when the service restarts
restart() {
# Run the process in the background (&), and direct errors to a log file
sh /user/start_everything.sh >/var/log/init_canpy.log 2>&1 &
}
它調用的 /user/start_everything.sh 腳本如下所示:
#!/bin/sh
# Run the "find_config_data.py" Python program
/data/venv/bin/python3.6 /user/canpy/find_config_data.py
問題是 /data 位於單獨的硬盤分區上,因此 init.d 無法找到它。 我得到的錯誤如下:
line 4: /data/venv/bin/python3.6: not found
我的主分區上只剩下 20 MB 的空間,所以我必須將 Python 3.6 及其庫安裝到 /data 分區上,它有 2.5 GB 的空間。
如何讓 init.d 在 /data/venv/bin/python3.6 中找到我的 Python 二進制文件? 每次 Linux 啟動/重新啟動時,我絕對必須運行這個 Python 程序。 謝謝!!
以下是我的分區設置:
root@FATBOX:/tmp/log# df -h
Filesystem Size Used Available Use% Mounted on
/dev/root 476.2M 456.0M 20.2M 96% /
devtmpfs 512.0K 0 512.0K 0% /dev
tmpfs 247.7M 116.0K 247.6M 0% /tmp
tmpfs 512.0K 0 512.0K 0% /dev
/dev/mmcblk0p3 2.7G 50.2M 2.5G 2% /data
我想出了一個替代解決方案,它可以完成工作,並確保我的應用程序繼續運行(額外的好處)。
我安排一個進程檢查器腳本在 cron 中每分鍾運行一次:
#!/bin/sh
# Checking if /user/canpy/app.py is running
ps | grep -v grep | grep /user/canpy/app.py
if [ $? -eq 0 ]; then
echo "/user/canpy/app.py is running."
else
# If it is not running, start it!
echo "/user/canpy/app.py is not running. Starting it now"
/data/venv/bin/python3.6 /user/canpy/app.py >/var/log/main_app_canpy.log 2>&1 &
fi
這是位於 /etc/crontabs/root 的 crontab:
# Schedule check_running.sh to run every minute
* * * * * sh /user/check_running.sh >/var/log/check_running_canpy.log 2>&1 &
干杯,肖恩
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.