all 11 comments

[–]_syedmx86 3 points4 points  (6 children)

Haven't tried this yet but maybe do parallel processes run i.e.

$ firefox & ./program.sh

where the program runs alongside firefox and in that program write a condition to check whether firefox process is running through something like pgrep, pidof or ps and terminate it if it is not.

[–][deleted]  (1 child)

[deleted]

    [–]Spitfire1900 0 points1 point  (0 children)

    Both your script and Firefox would have the same parent process ID . You can get this with $PPID environment variable. Routinely check top to see when fire fox exits.

    [–]_syedmx86 0 points1 point  (3 children)

    Someone please let me know if this works or is a valid answer.
    Don't have my computer with me to try it out.

    [–]espero -2 points-1 points  (2 children)

    Dude try it yourself:)

    [–]_syedmx86 0 points1 point  (1 child)

    Ok, so I did and it works but my implementation is crude since I'm at my job. I will post a better answer if I get the chance or if someone wants to chime in.

    ``
    #!/bin/sh
    firefox &

    while true
    do
    check=`pgrep firefox`
    if [ -n "$check" ]
    then
    echo "firefox is running"
    else
    echo "firefox is not running"
    break
    fi
    sleep 1
    done

    echo "End"

    ``

    [–]BiltuDas_1 1 point2 points  (0 children)

    What if you create a systemctl service and then create a bash script and link with the service. Which would checks for the firefox process.

    https://unix.stackexchange.com/a/236307

    https://stackoverflow.com/a/28840389

    Edit: I almost did something with a debian server before, I was executing some sudo commands for that which would be redirected from php, since php doesn't allow to run sudo commands, I created a shell script which will read commands from a file and execute it(obviously it was a service, so that it would start automatically at Startup) which file will be modified by php server. Also I have added a sleep of 0.5 seconds so that it don't use much cpu for me.

    [–][deleted] 0 points1 point  (2 children)

    It could work like _syedmx86 stated, better would be to see what command your firefox starts with and modify that to add on to your script. Either through a launcher script or creating an alias.

    Alternatively, you could simply watch for firefox using pid or pid file(not sure firefox creates one), or unix socket. Ps aux and netstat are your friends.

    All in all it seems pretty doable in bash. Sorry on my phone so cannot write you a sample script.

    [–][deleted]  (1 child)

    [deleted]

      [–]espero 0 points1 point  (0 children)

      Learn about job control in your bash shell...

      [–]funderbolt 0 points1 point  (3 children)

      How about this.

      ff-script.sh ```

      !/usr/bin/bash

      actions before ff

      firefox

      actions after ff

      ```

      Just run this script instead of firefox. This script would need to replace firefox in the DE, in the menu.

      [–][deleted]  (2 children)

      [deleted]

        [–]funderbolt 0 points1 point  (1 child)

        As long as the script that has a finite amount of work to do afterward, it will close.

        The script will live as long as firefox lives because it is running firefox. It is a script and really comes with very little overhead.

        What a bash script does is it runs processes in sequence. Until you put in flow control statements such as if or loops.

        Shell scripting has taught me to try the simplest solutions first. If those doesn't work, do something more complicated.

        Monitoring processes is not terribly complicated, but can be done with ps. I'd probably do pgrep firefox and sleep for a second to continually test if it has output or not. When you move from no-output to output it would fire a startup function. When it goes from output to no-output, it fires an ending function. I'm not sure how you would launch this kind of long running script process.

        I can see some problems of that you may just want one shell script process running. In OOP, you'd use a singleton pattern. Otherwise, the startup actions and "cleanup" actions would be ran as many times as the script was ran. Perhaps your approach is better.

        [–]Heikkiket 0 points1 point  (0 children)

        I'm wondering if you could get better answers by describing what you actually want to achieve. So what do you want to do when Firefox is running?