0

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.

CTB
  • 23
  • 5
  • See See [Linux Script to check if process is running and act on the result](https://stackoverflow.com/q/20162678/4154375) and [Bash script to check running process](https://stackoverflow.com/q/2903354/4154375). – pjh May 14 '23 at 19:55

1 Answers1

2

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
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223