How to send `SIGINT` over `systemctl` to custom daemon/service

Issue

This Content is from Stack Overflow. Question asked by mxwQuestion

I have a script that I would like to run as service on my Linux machine, let’s call it my_script.sh. It is executable and can be run over ./path/to/my_scrit.sh.

I also have script that kills my_script.sh, let’s call it kill_my_scprit.sh. The contents of kill_my_script.sh are:

#!/bin/bash

sudo kill -SIGINT $(pgrep my_script)

In essence, this should mimic Ctrl+C for my_script.sh.

This works perfectly fine if I run the scripts from a terminal, i.e. in ttyX I run ./path/to/my_scrit.sh to start it and in ttyY ./path/to/kill_my_scrit.sh to initiate the shut down sequence of my_script.sh.

As mentioned before, the goal is to run this as deamon, so I created /etc/systemd/system/my_script.service

[Unit]
....

[Service]
Type=simple
User=root
WorkingDirectory=/path/to/dir/
ExecStart=/path/to/my_script.sh
ExecStop=/path/to/kill_my_script.sh
Restart=on-failure

[Install]
...

Using sudo systemctl start my_script.service starts the script as expected, but using sudo systemctl stop my_script.service starts the shutdown sequence for my_script.sh, but doesn’t let it finish… Am I missing something? The shutdown sequence takes roughly 10sec…



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?