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

Asked

Viewed 537 times

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 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 all the configuration and open another window again, showing the Laravel home page.

bash stops in the browser opening process 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 last thing I can’t do is close the browser in the first process automatically as it should happen with the kill $PID.

Where am I going wrong?

1 answer

1

Apparently its variable PID is not receiving the id of the process generated in the browser opening. It is a bit difficult to use the Jobs -p in this situation as 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.