I edit code and LaTeX and like t开发者_C百科o 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?
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.
If you use that scriptlet often, put it in a script. Then, you can identify the script name in the process list.
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.
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
.
精彩评论