Need help writing a shell script

I need a shell script, loaded at start-up, running in the background, that will listen out to see if process A is running. If process A is running, then I need the shell script to kill process B for the duration that process A is running. Then, if process A stops running, the shell script needs to restart process B

So, logically, I guess:

if process A is running
stop process B
else
start process B

However, I need this shell script to be available to make the above “decisions” on a continual basis. In other words, if process A has been run during a session and the shell script has carried out its commands (closed process B or started process B), if I decide to run process A again later on in a session, I need this shell script to be able to continually run it’s commands (to stop or start process B) accordingly.

So, I am guessing some kind of loop

process A is defined as: agisoft-photoscan.sh (this a shell script that loads a virtualbox xp vm in full screen with agisoft photoscan open and ready to go)

process B is defined as: compton

In plain English, if this VM is running Compton needs to be switched off and if this VM is not running, Compton needs to be switched on.

At the moment, I am manually switching Compton on and off when I need to. but, it would be really nice to automate it.

Could really do with some help here in terms of how to actually write this script out.

Cheers folks

Maybe something like that:

#! /bin/bash 

while true; do
    VBOX=`ps axu | grep "[a]gisoft-photoscan.sh" | wc -l`
    COMP=`ps axu | grep "[c]ompton" | wc -l`
    echo $VBOX $COMP
    if [ $VBOX -eq 1 ]; then
        if [ ! $COMP -eq 0 ]; then
            killall compton
            echo "compton killed"
        fi
    else
        if [ $COMP -eq 0 ]; then
            compton &
            echo "compton started"
        fi
    fi
    sleep 1
done

In a startup application (echo lines to be removed at your discretion.

1 Like

Thanks for that. sadly not worked. I ran it in a terminal, The output was only:

0 5

This repeated every second on a new line

Weird, that means ps axu | grep “[c]ompton” detects 5 processes running with “compton” in their name.
If those are all really compton instances the slight edit I just made should fix it.

1 Like

still the same continual output I am afraid

0 5

Just a suggestion why not add killall compton to the start of agisoft-photoscan.sh then start it again /usr/bin/compton & at the end of the script? This would save on processor cycles that could be used by the agisoft application.

2 Likes

Please someone shoot me now for not thinking of that…:smile:

I’ve just done as suggested. Worked immediately

cheers ramblinman41

1 Like