How do I make a bash script to start running when Firefox starts and stop running when Firefox is closed?

huangapple go评论59阅读模式
英文:

How do I make a bash script to start running when Firefox starts and stop running when Firefox is closed?

问题

I have a script on my Fedora PC that I want to run only while Firefox is active.
我有一个在我的Fedora电脑上的脚本,我只想在Firefox处于活动状态时运行。

I want to find a way to monitor the PC for Firefox's status.
我想找到一种监视PC上Firefox状态的方法。

Once Firefox starts, I want the script to start.
一旦Firefox启动,我希望脚本也启动。

When Firefox is closed, I want the script to stop.
当Firefox关闭时,我希望脚本停止。

If Firefox is reopened in the same session, I want the script to run again, and repeat the process as many times as necessary.
如果在同一会话中重新打开Firefox,我希望脚本再次运行,并重复该过程多次。

Let's call the script "bash_script.sh". For example:
让我们称这个脚本为"bash_script.sh",例如:

#!/bin/sh
Start monitoring FF status
当监视FF状态
When FF starts, do something
当FF启动时,执行某些操作
When FF is closed, stop doing it and continue monitoring FF status
当FF关闭时,停止操作并继续监视FF状态

So I have the following questions:
所以我有以下几个问题:

  1. Is there a way to achieve this with a bash script? Can you please give me an example?
  2. 是否可以使用bash脚本实现这一点?你能给我一个例子吗?
  3. Should I change the permissions to 777 and put this script on crontab?
  4. 我应该将权限更改为777并将此脚本放在crontab中吗?
  5. If yes, what syntax should I use? If no, how else can I achieve that process?
  6. 如果是的,我应该使用什么语法?如果不是,我还能以其他方式实现这个过程吗?

I am a complete beginner with bash scripting and I have no idea where to start!
我完全是bash脚本的初学者,我不知道从何开始!
Apologies if this is a very easy question.
如果这是一个非常简单的问题,我道歉。

英文:

I have a script on my Fedora PC that I want to run only while Firefox is active.
I want to find a way to monitor the PC for Firefox's status.

Once Firefox starts, I want the script to start.
When Firefox is closed, I want the script to stop.
If Firefox is reopened in the same session, I want the script to run again, and repeat the process as many times as necessary.

Let's call the script "bash_script.sh". For example:

#!/bin/sh
Start monitoring FF status
When FF starts, do something
When FF is closed, stop doing it and continue monitoring FF status

So I have the following questions:

  1. Is there a way to achieve this with a bash script? Can you please give me an example?
  2. Should I change the permissions to 777 and put this script on crontab?
  3. If yes, what syntax should I use? If no, how else can I achieve that process?

I am a complete beginner with bash scripting and I have no idea where to start!
Apologies if this is a very easy question.

答案1

得分: 2

这只是一个包装脚本,可以在/usr/bin之前的PATH中访问:

firefox

#!/bin/bash

在运行FF之前的代码
/usr/bin/firefox & pid=$!
wait $pid
在运行FF之后的代码

如果要跟踪syscalls,可以使用:

strace -ff -s 1000 -o /tmp/trace-FF /usr/bin/firefox
英文:

Simply use this wrapper script accessible before /usr/bin in your PATH:

firefox:

#!/bin/bash

code_before_running_FF
/usr/bin/firefox & pid=$!
wait $pid
code_after_running_FF

If you want to track syscalls, you can use:

strace -ff -s 1000 -o /tmp/trace-FF /usr/bin/firefox

huangapple
  • 本文由 发表于 2023年5月15日 03:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定