脚本结束后如何运行进程并退出?

编程入门 行业动态 更新时间:2024-10-09 22:15:44
本文介绍了脚本结束后如何运行进程并退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我问了一个问题如何运行来自 python 的剪贴板清除命令和我得到了一个很好的答案:

subprocess.run("xclip",stdin=subprocess.DEVNULL) subprocess.run(["xclip","-selection","clipboard"],input="")

这似乎在 python 中工作,但有一个问题,它使进程处于打开状态.实际上它打开了 2 个进程,一个 xclip 和一个带有 xclip -selection clipboard 参数.

This seem to work in python, but there is a problem, it leaves the process open. Actually it opens 2 processes, one xclip and one with xclip -selection clipboard parameters.

而且它们似乎是僵尸进程,它们无限期地保留在那里,直到您再次复制粘贴某些内容.之后两人都消失了.

And it seems like they are zombie processes, they remain there indefinitely, until you copy-paste something again. After that both of them dissapear.

所以我从 USB 驱动器的目录运行脚本,它不允许您移除 USB 驱动器,它会显示USB 驱动器忙",直到进程没有关闭.

So I have run the script from an USB drive's directory, and it doesn't let you remove the USB drive, it says "USB drive busy", until the processes don't close.

所以要么我把新的东西复制到剪贴板,否则这个过程会无限期地留在那里,就像一个僵尸.

So either I copy something new into the clipboard, otherwise the process remains there indefinitely, like a zombie.

是否可以在 python 脚本结束后关闭进程?因为在 python 脚本运行后,该进程没有理由保持打开状态.

Is it possible to just close the process after the python script ends? Since there is no reason for that process to remain open after the python script has ran.

推荐答案

我必须阅读 xclip 的源代码以查看父级(您的直接子级)是否等待选择完成声称退出.如果没有,如果你杀了任何东西,你就会与实际工作竞争.您可以使用 xsel -c (正如在另一个问题中所建议的那样);解决只是卸载问题,你可以简单地改变孩子的当前目录:

I'd have to read xclip's source to see if the parent (your direct child) waits until the selection has been claimed to exit. If not, you would race with the actual work if you killed anything. You can use xsel -c (as was suggested in the other question); to solve just the unmounting problem, you can simply change the current directory of the child:

subprocess.run("xclip",stdin=subprocess.DEVNULL,cwd="/")

更多推荐

脚本结束后如何运行进程并退出?

本文发布于:2023-11-16 02:17:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   进程   结束后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!