标签: pgrep

如果应用程序正在运行,我如何检查 Bash 脚本?

我的主文件夹中有一个 gnome-run 应用程序。我现在已经添加了当我按下Meta+时运行的应用程序R(我在CCSM 中添加了它)。我通过./gnome-run在我的主文件夹中执行来运行应用程序。
我在ps -A.

问题是,如果我打开 gnome-run 程序并按下组合键,我希望应用程序关闭。有没有办法创建一个 bash 文件来检查应用程序是否正在运行?如果它然后关闭它,否则启动它。

bash gnome process-management pgrep

6
推荐指数
1
解决办法
3136
查看次数

为什么“pgrep firefox”在 Ubuntu 20.04 上不起作用

正如标题所示,在 Ubuntu 20.04 上执行以下命令:

pgrep firefox
Run Code Online (Sandbox Code Playgroud)

...没有返回结果。

同时,执行以下命令:

ps aux |grep firefox
Run Code Online (Sandbox Code Playgroud)

...返回 6 个结果,其中 5 个应匹配pgrep firefox。Ubuntu 上的原始命令有什么问题pgrep

ubuntu     81646  1.9  7.9 3063696 300208 ?      Sl   13:12   0:29 /usr/lib/firefox/firefox -new-window
ubuntu     81891  0.2  3.8 2473448 144796 ?      Sl   13:12   0:03 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 222536 -parentBuildID 20200720193547 -appdir /usr/lib/firefox/browser 81646 true tab
ubuntu     82020  0.1  3.7 2438972 141396 ?      Sl   13:12   0:02 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 897 -prefMapSize 222536 …
Run Code Online (Sandbox Code Playgroud)

grep ubuntu ps firefox pgrep

6
推荐指数
2
解决办法
1759
查看次数

为什么我不能在 bash 脚本上将 pgrep 输出正确地输出到变量?

我正在尝试制作一个脚本,要么在 compton 正在运行时退出它,要么在它没有运行时启动它。我从 man 那里读到如果找到进程它应该退出 1,所以我试图制作一个使用它的脚本......但是这不起作用,如果它关闭但不关闭它就会启动. 我究竟做错了什么 ??

#!/bin/bash


status=$(pgrep compton 2>&1)

if [[ $status == 1 ]];
    then
        killall compton
    else
        exec compton -b
fi

echo $status
Run Code Online (Sandbox Code Playgroud)

bash stdout stderr pgrep exit-status

5
推荐指数
2
解决办法
3414
查看次数

Cron 作业无法运行(pgrep 问题)

我每分钟运行一个 cron 作业,它启动一个 bash shell 脚本:

# Automatically reboot the server if it goes down
* * * * * /root/auto-restart.sh
Run Code Online (Sandbox Code Playgroud)

我可以手动运行该脚本,但每当我尝试通过 cron 作业运行它时,它都不会运行。我调试了一下,发现这个命令是罪魁祸首:

pgrep -fcl auto-restart.sh
Run Code Online (Sandbox Code Playgroud)

总而言之,当手动运行时,该命令返回正确的值,并且仅在脚本当前正在运行时列出 auto-restart.sh(正如它应该的那样)。

但是当 cron 作业启动它时,它会显示如下输出pgrep -fl auto-restart.sh(当它运行时):

3110 sh
3111 auto-restart.sh
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

scriptcheck=`pgrep -fcl auto-restart.sh`
if [ $scriptcheck -gt 1 ]; then
    echo "auto-restart.sh script already in use. Terminating.."
    #cron job debugging..
    echo "[DEBUG] scriptcheck returning greater than 1" > /tmp/debug.output
    echo "[DEBUG] Value of scriptcheck: ${scriptcheck}" >> /tmp/debug.output
    echo "[DEBUG] …
Run Code Online (Sandbox Code Playgroud)

cron shell-script pgrep

3
推荐指数
1
解决办法
2381
查看次数

Autokey - 如果正在运行则聚焦应用程序窗口,如果没有则启动应用程序

pcmanfm我正在尝试获取这样的processID :

pgrep -f "pcmanfm"
Run Code Online (Sandbox Code Playgroud)

pcmanfm不运行时,上面的命令不会返回任何内容(正如我所期望的那样)。

但是,当我从 python 运行命令时,即使pcmanfm未运行,它也会返回进程 ID:

processID = os.system('pgrep -f "pcmanfm"')
Run Code Online (Sandbox Code Playgroud)

此外,如果您在 python3 提示符下多次运行上述命令,它每次都会返回不同的 processID。一直以来,pcmanfm在这些命令之前已经关闭。

>>> processID = os.system('pgrep -f "pcmanfm"')
17412
>>> processID = os.system('pgrep -f "pcmanfm"')
17414
>>> processID = os.system('pgrep -f "pcmanfm"')
17416
Run Code Online (Sandbox Code Playgroud)

pcmanfm如果它当前没有运行,这确实会扰乱我的启动能力。我的脚本认为它正在运行,但实际上它没有运行。

为什么会发生这种情况?

实际上,我在尝试根据我观看的视频编写的自动键脚本中遇到了这个问题。这是我当前的脚本:

processID = system.exec_command('pgrep -f "pcmanfm" | head -1',True)
dialog.info_dialog("info",processID)

if (processID):
    cmd = "wmctrl -lp | grep " + processID + " | awk '{print $1}'" …
Run Code Online (Sandbox Code Playgroud)

bash process-management python3 pgrep autokey

2
推荐指数
1
解决办法
966
查看次数