如何识别正在运行的bash脚本?

发布于 2024-12-09 08:36:40 字数 326 浏览 0 评论 0原文

我编辑代码和 LaTeX,喜欢定期编译以检查编辑是否符合我的要求。我通常运行一个小的 bash 命令行,看起来像“while true; do make -s foo.pdf; sleep 2; done &”,然后编辑 foo.tex 并在 xpdf 中重新加载结果来检查它。每次我保存编辑时,脚本都会生成 pdf 文件,我可以检查我的编辑是否达到了预期效果。然而,在我完成其中几个操作后,结束编辑并继续,我的这些小脚本仍在运行,并且我启动的每个脚本可能会关闭。 ps 只显示脚本的睡眠部分,因此很难知道启动它的正确父级。我如何知道我看到的所有睡眠中的哪个父级是要杀死的,即如何判断哪个睡眠与我已完成的 make foo 相关?

I edit code and LaTeX and like to do periodic compilation to check whether edits are doing what I want. I usually run a small bash command line that looks like "while true; do make -s foo.pdf; sleep 2; done &", and then edit foo.tex and reload the result in xpdf to check it. Every time I save my edit, the script makes the pdf and I can check if my edits do what they want. However, after I have a couple of these going, end my editing and move on, I have these little scripts that are still running, and the xterm I started each in may be closed. ps only shows up the sleep part of the script, making it difficult to know the correct parent that started it. How can I know which parent of all the sleeps I see is the one to kill, ie, how to tell which sleep is associated with the make foo I am done with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Hello爱情风 2024-12-16 08:36:40

您可以使用 pstree 或 'ps -afjx' ,它们都显示进程树,这使得可以轻松识别父进程。

您还可以将父 PID 打印到每个脚本中的临时文件中,并在脚本完成后删除该文件。

you could use pstree, or 'ps -afjx' , which both show a tree of processes, which makes it easy to identify the parent.

you could also print out the parent PID into a tmp-file in each scrip -- and delete that file once the script is finished.

折戟 2024-12-16 08:36:40

如果您经常使用该 scriptlet,请将其放入脚本中。然后,您可以在进程列表中识别脚本名称。

If you use that scriptlet often, put it in a script. Then, you can identify the script name in the process list.

白昼 2024-12-16 08:36:40

如果我执行 bash -c "while true; do make -s foo.ps; sleep 2; done",则 ps 显示的 bash 命令的文件名是 I需要识别脚本。因此,我应该使用命令行中的命令启动 bash,而不是直接从终端命令行运行脚本!感谢威廉(我认为是珀塞尔),他的帖子让我思考正确的方向! Mnm Cee 建议使用一个包含适当 pid 的锁定文件,但创建该文件将涉及比在命令行上轻松完成的更多输入。可能必须编写一个单独的函数然后调用它。

If I do bash -c "while true; do make -s foo.ps; sleep 2; done", then the bash command command shown by ps has the filename I need to identify the script. So, instead of running the script directly from the terminal command line, I should start a bash with the command in the command line! Thanks to William (I think it was Pursell), whose post got me thinking on the right lines! Mnm Cee suggested a lockfile with the appropriate pid in it, but creating that would involve more typing than is easily done on the command line. Probably have to write a separate function and then call that.

星星的軌跡 2024-12-16 08:36:40

在输入命令的 shell 中,只需输入 jobs ,while 循环就会出现在列表中。假设括号中的作业编号为 3,则输入 kill %3 将其杀死。

或者,在您的 ps 标志中包含 jps j 可能是最方便的,因为它还会选择 BSD 对所有具有终端的进程的默认选择),记下进程组 ID (PGID),假设它是 1234,然后执行 kill -- -1234

In the shell where you entered the command, simply type jobs and the while loop should appear in the list. Suppose the job number in brackets is 3, then type kill %3 to kill it.

Alternatively, include j in your ps flags (ps j is probably most convenient as it will also choose BSD's default selection of all your processes that have terminals), take note of the process group ID (PGID), say it is 1234, then do kill -- -1234.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文