简体   繁体   English

作为启动脚本运行时结束Python脚本?

[英]End Python Script when running it as boot script?

I am using Debian and I have a python script that I would like to run during rc.local so that it will run on boot. 我正在使用Debian,我有一个想在rc.local期间运行的python脚本,以便它将在启动时运行。 I already have it working with a test file that is meant to run and terminate. 我已经可以使用要运行和终止的测试文件了。

The problem is that this file should eventually run indefinitely using Scheduler. 问题在于,该文件最终应使用Scheduler无限期运行。 It's job is to do serial reads, a small amount of processing on those reads, and inserts into a MySQL database. 它的工作是进行串行读取,对这些读取进行少量处理,然后插入到MySQL数据库中。 However, I am nervous about then not being able to cancel the script to get to my login prompt if changes need to be made since I was unable to terminate the test script early using Ctrl+C (^C). 但是,我担心如果无法进行更改就无法取消脚本以进入登录提示,因为我无法使用Ctrl + C(^ C)提前终止测试脚本。

My hope is that there is some command that I am just missing that will accomplish this. 我的希望是,我只是缺少一些命令来完成此操作。 Is there another key command that I'm missing that will terminate the python script and end rc.local? 我是否缺少另一个将终止python脚本并结束rc.local的关键命令?

Thanks. 谢谢。

EDIT: Another possible solution that would help me here is if there is a way to start a python script in the background during boot. 编辑:另一个可能对我有帮助的解决方案是,如果有一种方法可以在启动过程中在后台启动python脚本。 So it would start the script and then allow login while continuing to run the script in the background. 因此它将启动脚本,然后在继续在后台运行脚本的同时允许登录。

I'm starting to think this isn't something that's possible to accomplish so other suggestions to accomplish something similar to what I'm trying to do would be helpful as well. 我开始认为这是不可能完成的事情,因此,完成与我正在尝试的工作类似的其他建议也将有所帮助。

Thanks again. 再次感谢。

Seems like it was just a dumb mistake on my part. 看来这只是我的愚蠢错误。

I realized the whole point of this was to allow the python script to run as a background process during boot so I added the " &" to the end of the script call like you would when running it from the shell and viola I can get to my password prompt by pressing "Enter". 我意识到,这样做的全部目的是允许python脚本在引导过程中作为后台进程运行,因此我像在shell和中提琴中运行它一样,在脚本调用的末尾添加了“&”通过按“ Enter”输入我的密码提示。

I wanted to put this answer here just in case this would be something horribly wrong to do, but it accomplishes what I was looking for. 我想把这个答案放在这里,以防万一这是一件非常错误的事情,但是它实现了我想要的。

Making scripts run at boot time with Debian 使用Debian在启动时运行脚本

  1. Put your script in /etc/init.d/ . 将您的脚本放在/etc/init.d/ So, if your script is in a file called my_script , it should be located at /etc/init.d/my_script . 因此,如果您的脚本位于名为my_script的文件中,则应位于/etc/init.d/my_script

  2. Run update-rc.d my_script defaults as root. update-rc.d my_script defaults身份运行update-rc.d my_script defaults

  3. Don't forget to make your script executable and include the shebang. 不要忘记使脚本可执行,并包含shebang。 That means the first line of the script should be #!/usr/bin/python . 这意味着脚本的第一行应该是#!/usr/bin/python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM