在操作系统启动时运行 Python 脚本

Sij*_*tha 8 startup python init.d scripts init

这可能是一个重复的问题,但我找不到正确的方法来做到这一点。

我的情况是什么?->我想运行一个 Python 脚本。

python 脚本的作用是什么?->python 脚本运行以设置虚拟专用网络。

我目前如何手动启动它?->我使用 ./file.py start (这有效)

什么不工作?->当我尝试在开始(引导)中开始时,它什么也不做。我正在使用根本不起作用的 cron 作业,我不想使用它,我想使用 /etc/init.d/ 。

这是我在 /etc/init/xxx.conf 中的 file.conf

description "file start script"
author "sijan <sijanshrestha2@gmail.com>"



exec python file.py start
exec sleep 10
exec ifconfig ip0 11.0.2.251

exec ip=`ifconfig ip0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`

exec echo $ip >>/tmp/ip.log
Run Code Online (Sandbox Code Playgroud)

我在 /etc/init.d/file.py 中添加了 python 文件

我可能没有遵循正确的程序来执行此操作,因为我对系统级别非常陌生,但是非常感谢有关如何进行的任何想法。我非常渴望解决这个问题并学习

Ser*_*nyy 13

将脚本放入/etc/rc.local. 系统启动时,那里的脚本以 root 身份运行。正如您在评论中指定的那样,它也适用于 Raspberry Pi。

在你的情况下,你想运行它 python /path/to/script.py &

这是我的示例rc.local文件,我使用相同的方法运行电池和温度监控脚本

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/home/xieerqi/bin/batmon.sh &
/home/xieerqi/bin/preventShutdown.sh &
/home/xieerqi/bin/sh/temperature.sh  &
Run Code Online (Sandbox Code Playgroud)

附加信息 /etc/rc.local