Closing the terminal should kill the associated process with the terminal

317 views Asked by At

I have packaged a Debian file of our software. Now there is a .sh script that needs to be started to run the program/software. This .sh script actually runs a Django server and few more services.

To actually start this application, we need to run the .desktop file in the menu. This .desktop file in the menu is associated with the .sh script mentioned above. This prompts the terminal and asks for the password. Once the password is given, this will start the services and the terminal stays active.

To close this service completely, we need to kill the process by finding the PID of the process and killing it from the terminal. But now I want to kill this process when I close the terminal.

How can I do that?

2

There are 2 answers

0
umläute On

If you are trying to create a service (some program that runs in the background), you should create use your system's mechanism for this. The traditional one, would be a scrip in /etc/init.d/, a more modern approach is to use systemd.

E.g. a file /etc/systemd/system/myservice.system

[Unit]
Description=My Service

[Service]
Type=simple

# you could run the service as a special user
#User=specialuser
WorkingDirectory=/var/lib/myservice/
# execute this before starting the actual script
#ExecStartPre=/usr/lib/myservice/bin/prestart.sh
ExecStart=/usr/bin/myservice
Restart=on-failure

[Install]
WantedBy=multi-user.target

You can then start/stop the service (as root) using:

 systemctl start myservice

resp.

 systemctl stop myservice

You can have dependency chains of services, so starting myservice will automatically start myhelper1 and myhelper2.

Checkout the manpage systemd.unit.5

4
the paul On

When the controlling terminal is closed, the foreground process group should receive a SIGHUP signal. If your target process is already expected to be in the foreground, then it may be that it is catching or ignoring SIGHUP (the default behavior for a process receiving that signal is to terminate).