如何识别正在运行的bash脚本?
我编辑代码和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 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.
如果您经常使用该 scriptlet,请将其放入脚本中。然后,您可以在进程列表中识别脚本名称。
If you use that scriptlet often, put it in a script. Then, you can identify the script name in the process list.
如果我执行 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 byps
has the filename I need to identify the script. So, instead of running the script directly from the terminal command line, I should start abash
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.在输入命令的 shell 中,只需输入
jobs
,while 循环就会出现在列表中。假设括号中的作业编号为 3,则输入kill %3
将其杀死。或者,在您的 ps 标志中包含
j
(ps 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 typekill %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 dokill -- -1234
.