title: Linux: Make systemd kill a running Python script immediately?
body: <p>I am using <code>systemd</code> on Raspbian to run a Python script <code>script.py</code>. The <code>my.service</code> file looks like this:</p>
  
  <pre><code>[Unit]
  Description=My Python Script
  Requires=other.service
  
  [Service]
  Restart=always
  ExecStart=/home/script.py
  ExecStop=/home/script.py
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>When the <code>Required=other.service</code> stops, I want <code>my.service</code> to stop immediately and also terminate Python process running <code>script.py</code>.</p>
  
  <p>However, when trying this out by stopping <code>other.service</code> and then monitoring the state of <code>my.service</code> using <code>systemctl</code>, it seems like it takes good while for <code>my.service</code> to actually enter a '<strong><em>failed</em></strong>' state (stopped). It seems that calling <code>ExecStop</code> <em>to the script</em> is not enough to terminate <code>my.service</code> itself and the subsequent <code>script.py</code> in a minute manner.</p>
  
  <p>Just to be extra clear: I want the script to terminate pretty <em>immediately</em> in a way that is analogous to <kbd>Ctrl</kbd> + <kbd>C</kbd>. Basic Python clean-up is OK, but I don't want <code>systemd</code> to be waiting for a 'graceful' response time-out, or something like that.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>Is my interpretation of the delay correct, or is it just <code>systemctl</code> that is slow to update its status overview?</li>
  <li>What is the recommendable way to stop the service and terminate the script. Should I include some sort of <code>SIGINT</code> catching in the Python script? If so, how? Or is there something that can be done in <code>my.service</code> to expedite the stopping of the service and killing of the script?</li>
  </ol>