Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While I would still like a solution that makes scripting languages both cross-platform and easy-to-monitor from the command line, if you're just looking for an alternative to <code>killall &lt;scriptname&gt;</code> to stop custom services, here's how I solved it:</p> <pre><code>kill `ps -fC &lt;interpreterName&gt; | sed -n '/&lt;scriptName&gt;/s/^[^0-9]*\([0-9]*\).*$/\1/gp'` </code></pre> <p>For those not too familiar with ps and regexes, <code>ps</code>'s <code>-f</code> modifier has it list out a "full" set of information about a process, including its command-line arguments, and <code>-C</code> tells it to filter the list to only commands that match the next command-line argument. Replace <code>&lt;interpreterName&gt;</code> with <code>python</code> or <code>node</code> or whatever.</p> <p><code>sed</code>'s <code>-n</code> argument tells it to not print anything by default, and the regex script has to explicitly indicate that you want to print something.</p> <p>In the regex, the first <code>/&lt;scriptName&gt;/</code> tells it to filter its results to only lines that contain the interior regex. You can replace <code>&lt;scriptName&gt;</code> with <code>envtest</code>, for example.</p> <p>The <code>s</code> indicates that a substitution regex will follow. <code>/^[^0-9]*\([0-9]*\).*$/</code> being the line matching portion and <code>/\1/</code> being the substitution portion. In the line matching portion, the <code>^</code> at the very beginning and the <code>$</code> at the very end mean that the match must start from the beginning of the line and end at the end of the line -- the entire line being checked is to be replaced.</p> <p>The <code>[^0-9]*</code> involves a few things: <code>[]</code> are used to define a set of allowable characters. Within this portion of the regex, the dash <code>-</code> means a range of characters, so it expands to <code>0123456789</code>. The <code>^</code> here mean "not" and immediately means "match any character that is NOT a number". The asterisk <code>*</code> afterwards means to keep on matching characters in this set until it encounters a non-matching character, in this case a number.</p> <p>The <code>\([0-9]*\)</code> has two portions, the <code>\(\)</code> and <code>[0-9]*</code>. The latter should be easy to follow from the previous explanation: it matches only numbers, and grabs as many as it can. The <code>\(\)</code> mean to save the contents of what is matched to a temporary variable. (In other RegEx versions, including Javascript and Perl, <code>()</code> is used, instead.)</p> <p>Finally, the <code>.*</code> means to match every remaining character, as <code>.</code> means any possible character.</p> <p>The <code>/\1/</code> portion says to replace the matched portion of the line (which is the whole line in this case) with <code>\1</code>, which is a reference to the saved temporary variable (if there had been two <code>\(\)</code> sections, the first one in the RegEx would be <code>\1</code> and the second <code>\2</code>).</p> <p>The <code>g</code> afterwards mean to be "greedy" and run this matching code on every line encountered, and the <code>p</code> means to print any line that has reached this point.</p> <p>Technically, this will blow up if you have multiple copies of your script running, and you'd really want the slightly heavier:</p> <pre><code>ps -fC &lt;interpreterName&gt; | sed -n '/&lt;scriptName&gt;/s/^[^0-9]*\([0-9]*\).$/kill \1/gp' | bash </code></pre> <p>If you want to truly replicate kill*all* functionality, but this spawns a separate bash shell for each script you'd like to kill.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload