'How to kill selective child processes upon stop or restart of service

In the unit configuration file for systemd service, as mentioned below,

Inside the test_auto.tcsh script, there are 10 processes are being launched.

Upon service restart or stop, I want to kill only 2 selective processes out of 10. Not sure how can I achieve this from the systemd configuration settings.

KillMode=process make sure to kill only main process, on top of that, I want to kill 2 additional child process upon service termination, not all.

I tried to run a command (to kill the 2 selective processes) using ExecPostStop, but seems like this option doesn't work in Restart mode of service.

Any idea to get this functionality from the service settings ?

[Unit]
Description=test_auto

[Service]
Type=simple
ExecStart=/mydirectory/test_auto.tcsh

Restart=always
KillMode=process

[Install]
WantedBy=default.target


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source