如何在应用程序终止时执行脚本

use*_*037 4 scripts .desktop 16.04

要在应用程序启动时执行脚本,我可以更改Exec指令/usr/share/applications/App.desktop

当应用程序关闭时怎么办?该事件有任何钩子吗?

Ser*_*nyy 5

首先,通常认为在/usr/share/applications. 此外,它的效率非常低,因为它是作为包的一部分提供的,如果该包有更新,你猜怎么着?您的更改将被覆盖。如果您绝对 100% 需要进行更改,请创建一个自定义.desktop文件并将其存储在~/.local/share/applications

相反,我建议使用一个使用while循环的脚本并pgrep -f等待您想要的应用程序。例如,在下面的示例中,脚本使用firefox进程字符串。

#!/bin/bash

process_string="firefox"
while true
do

    while ! pgrep -f "$process_string"
    do
       echo "Waiting for $process_string"
       sleep 3
    done

    while pgrep -f "$process_string"
    do
       echo "Waiting for $process_string to exit"
       sleep 3
    done

    # place the command you want to occur when 
    # process exits here
sleep 3
done
Run Code Online (Sandbox Code Playgroud)

该脚本连续循环运行,因此您可以将其添加为启动应用程序的一部分,以便在登录时启动