Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在OpenWrt Linux中的单独分区上,在启动/重新启动时运行Python程序_Python_Python 3.x_Linux_Openwrt - Fatal编程技术网

在OpenWrt Linux中的单独分区上,在启动/重新启动时运行Python程序

在OpenWrt Linux中的单独分区上,在启动/重新启动时运行Python程序,python,python-3.x,linux,openwrt,Python,Python 3.x,Linux,Openwrt,在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

在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 &
}
#!/bin/sh

# Run the "find_config_data.py" Python program 
/data/venv/bin/python3.6 /user/canpy/find_config_data.py
line 4: /data/venv/bin/python3.6: not found
它调用的/user/start_everything.sh脚本如下所示:

#!/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 &
}
#!/bin/sh

# Run the "find_config_data.py" Python program 
/data/venv/bin/python3.6 /user/canpy/find_config_data.py
line 4: /data/venv/bin/python3.6: not found
问题是/data位于一个单独的硬盘分区上,因此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 &
}
#!/bin/sh

# Run the "find_config_data.py" Python program 
/data/venv/bin/python3.6 /user/canpy/find_config_data.py
line 4: /data/venv/bin/python3.6: not found
我的主分区只剩下20MB的空间,因此我必须将Python 3.6及其库安装到/data分区上,该分区有2.5GB的空间

如何让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/crontab/root的crontab:

# Schedule check_running.sh to run every minute
* * * * * sh /user/check_running.sh >/var/log/check_running_canpy.log 2>&1 &
干杯, 肖恩