Run Additional Commands on a Specific PID

I’m completely new with Linux so forgive me if this seems like an absurd question.

I am trying to open new tabs on Firefox without a url from the terminal. So my command is:

$ firefox -new-tab -search foo1

Now when I try to do it again in the hopes that it opens a new tab searching for a new item, say:

$ firefox -new-tab -search foo2

it opens a new window instead. So I had this thought that maybe if I could get the PID of the previous browser I could run the new tab command on that PID, and so the new tab actually opens on the same window. I can get the PID with relative ease if I run:

$ firefox -new-tab -search foo1 </dev/null >/dev/null 2>&1 &

But actually using the PID to execute a new command on that PID is what I completely have no idea of.

Any and all input is appreciated. Thanks!

Asked By: lems

||

The firefox browser you’re interacting with, no matter how many windows it shows, is only one "master" PID (with actual website workers running in their own processes and communicating with the main process through some well-defined interfaces), anyways. There’s no 1:1 correspondence between "window" and "PID".

So, sadly, working around that firefox command line bug (--search always opening a new window) won’t work the way you think it would.

I’ve thought about this, but there’s sadly no way to tell firefox to open a new tab and execute a query to the default search engine there, without writing a browser extension or instrumenting the browser in another program.

So, the best I can offer you is this:

  1. Pick the search engine of your choice, and note the query URL. (The easiest way is to search for a random string, say "qwert", and then mentally replace that with a placeholder, e.g. https://duckduckgo.com/?q=qwert becomes https://duckduckgo.com/?q=<placeholder>
  2. use that URL with --new-tab:
searchword="strange women lying in ponds distributing swords"
encoded="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${searchword}")"
firefox --new-tab "https://duckduckgo.com/?q=${encoded}"

There’s more than one way to weigh a duck, obviously. I would much have preferred to use firefox --new-tab "$(trurl --url 'https://duckduckgo.com/' --append "query=q=${searchword}")" over the Perl invocation, but trurl isn’t yet available on many Linux distros.

Anyways, no matter how you do it, this is not very nice to use. To make it nicer to use, you’ll want to put this in a text file called "websearch":

#!/bin/bash
searchwords="$@"
encoded="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${searchwords}")"
firefox --new-tab "https://duckduckgo.com/?q=${encoded}"

let’s assume it is in a directory called bin/ in your home directory; make it executable by running chmod a+x ~/bin/websearch. Now you can use it:

~/bin/websearch What is the air-speed velocity of an unladen swallow
Answered By: Marcus Müller
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.