linux - 关闭终端应该终止与终端相关的进程

标签 linux shell scripting debian deb

我已经打包了我们软件的 Debian 文件。现在有一个.sh 脚本需要启动来运行程序/软件。这个 .sh 脚本实际上运行一个 Django 服务器和一些其他服务。

要真正启动这个应用程序,我们需要运行菜单中的.desktop 文件。菜单中的这个 .desktop 文件与上面提到的 .sh 脚本相关联。这会提示终端并要求输入密码。一旦输入密码,这将启动服务并且终端保持事件状态。

要完全关闭此服务,我们需要通过找到进程的 PID 并从终端将其杀死来杀死该进程。但是现在我想在关闭终端时终止这个进程。

我该怎么做?

最佳答案

如果您正在尝试创建一个服务(一些在后台运行的程序),您应该使用您系统的机制来创建服务。 传统的方法是 /etc/init.d/ 中的脚本,更现代的方法是使用 systemd

例如一个文件 /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

然后您可以使用以下方式启动/停止服务(以 root 用户身份):

 systemctl start myservice

回复

 systemctl stop myservice

您可以拥有服务的依赖链,因此启动 myservice 将自动启动 myhelper1myhelper2

查看联机帮助页 systemd.unit.5

关于linux - 关闭终端应该终止与终端相关的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486847/

相关文章:

linux - Eclipse 不使用 .bashrc 中设置的路径

linux - ubuntu安装mpich时添加路径

bash - 为什么我不能在 while 循环中求值?

python - 如何让我的 Python 脚本使用 bash 工作?

linux - 脚本不保存新行或计算文件大小的问题

macos - 从文件尾部修剪 N 行的简单方法是什么(不使用 'head' )?

c++ - Linux上使用C的Char数组和指针进行段错误

linux - 按进程名称过滤并记录 CPU 使用情况

bash - unix bash shell 脚本中的惰性匹配

javascript - google api的platform.js和plusone.js有什么区别?