I've been using Ubuntu for a few weeks, and I want to know more about automating tasks.

How can I write a bash script to open a web browser with a specific URL after startup?

  • 1
    Hello, your question seems to say nothing (at least to me). It's ok you wish to learn but you may receive a better support if you tell us an example of what you wish to automate in order to get the best answer for you. Thank you! – Geppettvs D'Constanzo Dec 23 '13 at 19:11
  • Hi black, this question is very hard to read. I think you want to search for CRON jobs and you most definitely should take revise your question. – don.joey Dec 23 '13 at 19:12

For this case I am going to imagine a scenery. First of all let's say I need a script to open a web browser in certain Youtube Video(s) and each time it opens on boot I need the videos to be played with a random different duration.

First of all let's create the file, I am calling this "youtuviewer.sh", inside of which I am going to set this portions of code:

chromium-browser http://www.youtube.com/watch?v=7bLaLJ51rRk http://www.youtube.com/watch?v=OxYSaT_NfjQ &
echo $n
sleep $n
killall chromium-browser
echo "all done!"

I explain it quickly:

  1. Line 1: The executable will invoke bash to interpret the instructions after which
  2. Line 2: chromium-browser will run (it can be replaced by firefox or any other web browser). The browser will open the youtube links in the list in separated tabs, the list should be separated by a space. The ampersand (&) will instruct that after executing that line, the rest of the script should be executed and the chromium-browser instance should be left running meanwhile.
  3. Line 3: We are going to generate a random number, between 30 and 90 and store it in a variable called "n". This will be the number of seconds that we are going to use in the next lines.
  4. Line 4: We write on the terminal how many seconds will be used based on the random number.
  5. Line 5: We make the script to "sleep" the "n" number of seconds.
  6. Line 6: We kill chromium-browser in order to continue. This line will be executed only after Line 5 finishes waiting ("sleeping") the amount of seconds stored on the "n" variable.
  7. Line 7: We celebrate saying "all done!" in the terminal.

A more complex example can be made with this 7 lines, indeed. You can make it in just one line, everything depends on what you wish to achieve.

We are going to set execution permissions to the file via your favorite file browser (nautilus comes to my mind) or via terminal. See this: How do I run .sh files?

Now we need to setup the file to be run on boot, we can achieve this via cron (see this: How do I set up a Cron job?) or by using the "Startup applications" dialog (see this: How to add application to startup application menu?).

This is the simplest way I find to do what the edits by @ImaginaryRobots let's see.

If you need further assistance don't hesitate to let us know.

Good luck!

  • i tried your peice of code bt should i add my youtuviewer.sh file in cron.daily flder – black Mar 18 '14 at 5:59
  • when i am running .sh file via terminal like bash ~/Desktop/youtuviewer.sh its opening browser with specificlogin. – black Mar 18 '14 at 6:18

To open an URL you should use xdg-open, which will use your preferred browser to visit the URL.

$ xdg-open http://www.example.com/

In order to Open It after startup, you should do what Geppettvs D'Constanzo suggests:

  • A Cron Job
  • Startup Applications

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.