How to open and close a browser window programmatically using bash commands in ubuntu 16.04?

0

I have a bash code that, at the end of some installations, should automatically open a browser window with a URL (myapp.com) passed as a parameter. A php configuration page is displayed, confirming that everything has been installed correctly and after 30 seconds this window should close automatically, install a few more things (among them laravel) do the whole configuration and open another window again, showing the start page of Laravel.

Bash stops in the process of opening the browser and does not end. Following this POST , what I have so far is the following code:

...
PRJ_URL = 'myapp.com'
...
firefox $PRJ_URL & PID='jobs -p'& sleep 30s & kill $PID #OK-mostra a página do phpinfo() corretamente
... #instala mais algumas coisas
... #instala Laravel no folder desejado e configura as permissões
firefox $PRJ_URL #OK-mostra a página inicial do Laravel

Everything is working as expected. The only thing I can not do is close the browser in the first process automatically as it should with kill $PID .

Where am I going wrong?

    
asked by anonymous 26.12.2017 / 22:03

1 answer

1

Apparently your PID variable is not getting the process id generated at the opening of the browser. It's a bit tricky to use jobs -p in this situation because it displays the processes managed by the current session ( see more here ).

I would try a slightly simpler solution:

#!/bin/bash

url=$1
timeout=$2

sensible-browser $url & pid=$!
sleep $timeout

echo "Killing proccess["$pid"]..."
kill $pid

# installing some stuff

sensible-browser $url

Example:

./browser.sh https://google.com 5
    
27.12.2017 / 01:56