[英]Run Python program on bootup/reboot, on separate partition in OpenWrt Linux
On OpenWrt Linux, I put a startup script in the /etc/init.d folder, and enabled it.在 OpenWrt Linux 上,我将启动脚本放在 /etc/init.d 文件夹中,并启用它。 The script is fine, and looks as follows:脚本很好,如下所示:
#!/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 &
}
The /user/start_everything.sh script it calls looks as follows:它调用的 /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
The problem is that /data is on a separate hard drive partition, so init.d has problems finding it.问题是 /data 位于单独的硬盘分区上,因此 init.d 无法找到它。 The error I get is as follows:我得到的错误如下:
line 4: /data/venv/bin/python3.6: not found
My main partition only has 20 MB of space left on it, so I must install Python 3.6 and its libraries onto the /data partition, which has 2.5 GB of space.我的主分区上只剩下 20 MB 的空间,所以我必须将 Python 3.6 及其库安装到 /data 分区上,它有 2.5 GB 的空间。
How can I make init.d find my Python binary at /data/venv/bin/python3.6?如何让 init.d 在 /data/venv/bin/python3.6 中找到我的 Python 二进制文件? I absolutely must have this Python program run every time Linux boots/reboots.每次 Linux 启动/重新启动时,我绝对必须运行这个 Python 程序。 THANKS!!谢谢!!
The following is my partition setup:以下是我的分区设置:
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
I figured out an alternative solution, which gets the job done, and ensures my apps continue running (added benefit).我想出了一个替代解决方案,它可以完成工作,并确保我的应用程序继续运行(额外的好处)。
I schedule a process checker script to run every minute in cron:我安排一个进程检查器脚本在 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
Here is the crontab located at /etc/crontabs/root:这是位于 /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 &
Cheers, Sean干杯,肖恩
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.