Linux 在启动时停止 Raspberry Pi 运行程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17237630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop Raspberry Pi from running program at boot
提问by user2506445
I was trying to figure out how to run a program at boot, but after realizing this program is an infinite loop, I have no way of getting out, or back to the terminal. Right when I give the Pi power it just goes right to the program on a black background. I don't remember exactly what I did to make it run at boot, I believe I just added some code in a place right above something that said "exit 0" and below something that says "esac". I don't remember the command that even got me there. (I am new to the Pi and Python and have just been playing around..unsucessfully.)
我试图弄清楚如何在启动时运行程序,但在意识到这个程序是一个无限循环后,我无法退出或返回终端。当我给 Pi 供电时,它就直接进入黑色背景的程序。我不记得我做了什么让它在启动时运行,我相信我只是在上面写着“exit 0”和下面写着“esac”的地方添加了一些代码。我不记得让我到达那里的命令。(我是 Pi 和 Python 的新手,刚刚玩过……不成功。)
If anyone could help me either delete this program or get me to be able to edit it so I can fix the infinite loop that would be great. I'd rather not have to completely over-write the sd card with a fresh raspbian. But like I said I can't do anything at boot, and Ctrl + C doesn't do anything nor Ctrl + Alt + Delete.
如果有人能帮我删除这个程序,或者让我能够编辑它,这样我就可以修复无限循环,那就太好了。我宁愿不必用新的 raspbian 完全覆盖 SD 卡。但是就像我说的那样,我在启动时什么也做不了,Ctrl + C 和 Ctrl + Alt + Delete 也不做任何事情。
EDIT: When I put the SD card in my computer, I see a list of 11 files: bootcode cmdline config fixup fixup_cd issue kernel kernel_cutdown kernel_emergency start.elf start_cd.elf
编辑:当我将 SD 卡放入我的计算机时,我看到一个包含 11 个文件的列表: bootcode cmdline config fixup fixup_cd issue kernel kernel_cutdown kernel_emergency start.elf start_cd.elf
None of these mean anything to me...
这些对我来说都没有任何意义......
回答by hek2mgl
Update:Looks like I failed to get that you want to interrupt the running bootprocess and stop the script.
更新:看起来我没能得到您想要中断正在运行的引导过程并停止脚本的信息。
The simplest idea would be to turn of the pi, unplug the sd card and plug it into your desktop, (re)move the script and boot again.
最简单的想法是关闭 pi,拔下 SD 卡并将其插入桌面,(重新)移动脚本并再次启动。
Processes spawned during boot are stored in scripts in /etc/init.d/...
. These scripts are called by the init process, the first process on a Linux machine (PID:1)
启动期间产生的进程存储在 /etc/init.d/...
. 这些脚本由 init 进程调用,这是 Linux 机器上的第一个进程 (PID:1)
But init starts not all scripts in /etc/init.d
. This depends on the run level. On a debian system there are 7 run levels.
但是 init 并不是所有的脚本都在/etc/init.d
. 这取决于运行级别。在 debian 系统上有 7 个运行级别。
For every run level there is a folder called like:
对于每个运行级别,都有一个名为的文件夹,如下所示:
/etc/rc0.d
...
/rc6.d
in which are softlinks to scripts from /etc/init.d are stored.
其中存储了从 /etc/init.d 到脚本的软链接。
To remove a script from being executed on every boot you'll delete all links from that folders. Usually on debian systems this is done using the update-rc.d
tool:
要删除每次启动时执行的脚本,您将删除该文件夹中的所有链接。通常在 debian 系统上,这是使用以下update-rc.d
工具完成的:
update-rc.d NAME_OF_INIT_SCRIPT remove
You should also have a look at the file /etc/rc.local
你也应该看看文件 /etc/rc.local
回答by user2536152
You can use the cmdline.txt. First, add or modify the cmdline.txt file on your sd card. Add "init=/bin/sh", then restart your Pi, and you can see a command line prompt. Type 'sudo nano /etc/rc.local' to edit the file, and comment or delete the line containing the error. After that restart.
您可以使用 cmdline.txt。首先,在你的sd卡上添加或修改cmdline.txt文件。添加“init=/bin/sh”,然后重启你的树莓派,你可以看到命令行提示。输入“sudo nano /etc/rc.local”来编辑文件,并注释或删除包含错误的行。在那之后重新启动。
回答by Wout Standaert
I had exactly the same issue, couldn't quit the process using CTRL+C. I edited the cmdline.txt like stated above, but then the pi didn't load the necessary usb drivers for my keyboard.
我遇到了完全相同的问题,无法使用 CTRL+C 退出该过程。我像上面所说的那样编辑了 cmdline.txt,但是 pi 没有为我的键盘加载必要的 USB 驱动程序。
So eventually I logged in over SSH to the pi, and modified my rc.local file that way.
所以最终我通过 SSH 登录到 pi,并以这种方式修改了我的 rc.local 文件。
回答by Rambar48
I had the same problem as explained at the beginning of this Post. My Python app was caught in an endless loop.
我遇到了与本文开头所解释的相同的问题。我的 Python 应用程序陷入了无限循环。
I tried the Ctrl+Alt+F2Command as recommended in many posts found in Internet without any terminal window being open. Apparently, after many other trials and reboots I saw a glimpse of a Terminal window each time the loop of my application restarted. It was impossible to catch anything until I started recording the screen using the slow motion video of my mobile phone and, yes, a terminal window with the Linux prompt was active, and able to accept keyboard entries.
我试过Ctrl+ Alt+F2命令,如在网上查到的,没有任何终端窗口是否打开很多职位推荐。显然,经过许多其他试验和重新启动后,每次我的应用程序循环重新启动时,我都会看到终端窗口的一瞥。在我开始使用手机的慢动作视频录制屏幕之前,不可能捕捉到任何东西,是的,带有 Linux 提示的终端窗口处于活动状态,并且能够接受键盘输入。
All I did was (almost blindly) editing the culprit file with the
我所做的只是(几乎是盲目地)用
sudo nano filename command
and entering some characters in order to actually corrupt such file, saving and closing it, a rebooting the Pi. At the end of the boot, the file produced an error but the system kept ready to operate.
并输入一些字符以实际破坏此类文件,保存并关闭它,重新启动 Pi。在引导结束时,该文件产生错误,但系统保持准备运行。
I was then able to fix the bug in a normal way. It hope this may be useful to others. In my case it spared me of burning a new Raspbian and losing all my previous work.
然后我能够以正常方式修复错误。它希望这对其他人有用。就我而言,它使我免于烧毁新的 Raspbian 并丢失我以前的所有工作。
回答by user1070356
Alt + PrintScn + k to kill the process stuck running from rc.local
Alt + PrintScn + k 终止从 rc.local 运行的进程
回答by SudeepJD
I got myself stuck in exactly the same problem. Luckily I had the ssh enabled, apparently this is disabled by default on Raspbian Jessie, so this may not work for all.
我让自己陷入了完全相同的问题。幸运的是,我启用了 ssh,显然在 Raspbian Jessie 上默认禁用了此功能,因此这可能不适用于所有人。
The exit 0 is the line in /etc/rc.local where you would have added in the script that is now running in a continuous mode. If you can ssh into the Pi using Putty and the Pi's IP address then
出口 0 是 /etc/rc.local 中的行,您将添加到现在以连续模式运行的脚本中。如果您可以使用 Putty 和 Pi 的 IP 地址 SSH 进入 Pi,那么
sudo nano /etc/rc.local
Scroll down to the bottom of the file and remove the offending program, then sudo reboot
向下滚动到文件底部并删除有问题的程序,然后sudo reboot
They way to prevent this issue from happening is to add an & (ampersand) to the end of the line to fork the process and run it as a separate process like so
他们防止这个问题发生的方法是在行的末尾添加一个 &(&符号)来分叉进程并将其作为一个单独的进程运行,就像这样
python /home/pi/myscript.py &
as specified in https://www.raspberrypi.org/documentation/linux/usage/rc-local.md
如https://www.raspberrypi.org/documentation/linux/usage/rc-local.md 中所述
The ampersand allows the command to run in a separate process and continue booting with the process running.
&符号允许命令在单独的进程中运行,并在进程运行的情况下继续引导。
回答by Andrey Mazur
In my case worked combination "Alt" + "F4", it stops the current session and opens the login screen
在我的情况下,组合“Alt”+“F4”,它会停止当前会话并打开登录屏幕