简体   繁体   English

如何设置Laravel启动和停止python脚本

[英]How to setup Laravel to start and stop python scripts

I am testing my python script macro.py that runs in a loop from the terminal. 我正在测试从终端循环运行的python脚本macro.py

My plan is to code a Laravel application so that I can setup multiple instances of macro.py running on the server and these can be started and stopped at the any time. 我的计划是编写一个Laravel应用程序代码,以便我可以设置服务器上运行的macro.py多个实例,并且可以随时启动和停止这些实例。

Is this possible without too much difficulty? 这可能没有太多困难吗?

Invoke an external script the same way it is mentioned in this question . 以与本问题中提到的相同方式调用外部脚本。 It's very important to store the process pid in any volatile memory (database, file). 将进程pid存储在任何易失性内存(数据库,文件)中非常重要。 The retrieved pid number can be later used to stop the process using kill -9 process_pid command. 稍后可以使用kill -9 process_pid命令将检索到的pid号用于停止进程。

Be careful, if your python script "breaks" in the background (between your start and stop action called from application) there is a chance that the other process will retrieve the same pid number! 请注意,如果您的python脚本在后台(从应用程序调用的启动和停止操作之间)“中断”,则其他进程有可能会检索到相同的pid号! I recommend to store also the process startup time as well as pid number. 我建议还存储进程启动时间以及pid号。 Before killing the process do the additional check of the stored startup time and kill the process only if test passed (otherwise assume that the process stopped unexpectedly and show appropriate information in the user interface). 在终止该进程之前,请对存储的启动时间进行额外检查,并仅在测试通过后终止该进程(否则,假定该进程意外停止并在用户界面中显示适当的信息)。

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

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