Use xdg-open to open a url with a new process
Solution 1
Strange, it works like that out of the box on my Debian. Try running it in the background:
xdg-open http://www.google.com &
You can make this into a function by adding these lines to your ~/.bashrc
file:
function open () {
xdg-open "$*" &
}
You can then simply run open http://www.google.com
and it will run in the background.
Solution 2
If you want to detach the process from the current shell rather than starting it as a background job with xdg-open http://www.google.com &
, I like the detach
utility:
detach xdg-open http://www.google.com
One could create an alias for this. I like detach
over nohup
as closes stdin stdout and stderr by default so its invocation is cleaner.
Solution 3
xdg-open
waits for the program to finish. This is by design. If the program is a text mode program, it has to stay in the foreground in the terminal. Even if the program is a GUI one, this behavior is useful in case xdg-open
is used from a script and the script wants to perform something after the file has been edited (e.g. send the new version somewhere or otherwise make something with the new version).
If you don't want to wait, run xdg-open
in the background. You can run any shell command in the background by putting an ampersand at the end.
xdg-open http://www.google.com &
With some programs, xdg-open
returns immediately. What happens is actually that the program that xdg-open
invokes returns immediately. This typically happens with GUI programs that open all files in a single instance: when you start them a second time, they send a message the running instance to tell it to open the file, and exit immediately.
Solution 4
By combining terdon's answer and Dan D.'s discussion of nohup
I created a function which does exactly what I want:
- Opens the file in the chosen app without any writing to the terminal at all.
- Detaches the process so the terminal window can be closed.
- Doesn't write any errors the app produces to the terminal.
I added this to my .bashrc/.zshrc
file:
function open () {
nohup xdg-open "$*" > /dev/null 2>&1
}
For an explanantion of /dev/null 2>&1
see this discussion about hiding stdout
and stderr
.
Related videos on Youtube
Trong Nguyen
Updated on September 18, 2022Comments
-
Trong Nguyen almost 2 years
I'm starting to experiment with Crunchbang (which is based on Debian, and uses terminator) as a web development environment, and one of the things I am struggling with is the behaviour of
xdg-open
. I come from an OSX background, so forgive me if this question comes off as dense.I would like to be able to open a url with
xdg-open http://www.google.com
and then continue to use the same terminal window to work (it's howopen
functions in OSX). Right now, usingxdg-open
occupies the current tab/session until I close browser window, or manually end things withctrl + c
. I'd much prefer it start a new process, that way I can open up a URL, refer to data on the page, and use it in the same tab/window without needing to open an additional one.-
Cristian Ciupitu almost 10 yearsWhat web browser are you using?
-
-
Trong Nguyen about 11 yearsAlas,
detach
does not seem to exist in my distro.nohup
launches the process, but still occupies the terminal window. -
Trong Nguyen about 11 yearsThis works great. I was hoping to have a simple alias to to
open
, (i.e. open='xdg-open') is there a way to get the functionality ofxdg-open <url> &
without using a shell function? -
Trong Nguyen about 11 yearsThis still keeps the process running in the current terminal window. Let me know if I need to clarify my question.
-
slm about 11 yearsThe issue here isn't xdg-open, see @Gilles answer, he explains why xdg-open is being held up, it's the GUI you're sending the URL to which is causing xdg-open to wait.
-
slm about 11 yearsWhat browser are you sending the URL to? Is there a dialog box or anything else being popped when you send the URLs w/
xdg-open
? Seems like several of the answerers here have indicated that the normal behavior is that control is returned to the terminal after some period of time. -
Trong Nguyen about 11 yearsthanks for the clarification. I've reviewed the answers and I understand this better now. The browser is iceweasal.
-
slm about 11 yearsYeah that's essentially Firefox so just backgrounding the xdg-open should do the job. Is that not working as you expected? Also did you get your question resolved about "not doing this w/o using a shell function?" Wasn't sure entirely what you meant by that comment.
-
terdon about 11 years@NickTomlin Not as far as I know, no, but what have you got against functions? As you can see in my updated answer it is almost as simple as an alias.
-
Trong Nguyen about 11 yearsNot a particular bias, I was hoping to avoid a function since I am trying to use (and reuse) the same alias for both Mac OS and *nix flavors.
-
terdon about 11 yearsNo reason why you couldn't. Functions depend on the shell, as long as you use bash in all systems in question it should work perfectly well.
-
Cristian Ciupitu almost 10 yearsIt works out of the box on Fedora 20 too.
-
Gilles 'SO- stop being evil' over 8 years@NulledPointer Lock files, X11 window messages, D-Bus, … I suppose D-Bus is the “standard” Freedesktop way but I don't know if there is a formal specification for this.
-
Robbie over 7 yearsOn my Ubuntu 16.04 system, xdg-open always returns immediately. For my current application, I would actually prefer that it blocks. Where does your "xdg-open waits for the program to finish. This is by design." information come from?
-
Gilles 'SO- stop being evil' over 7 years@CharlBotha
xdg-open
returns immediately (I can confirm that on Ubuntu 16.04), but the program it invokes may move to the background. For example, on my system,xdg-open
invokes Evince for PDF files;evince foo.pdf
blocks until you close the PDF file, unless the PDF is already open, in which case the secondevince
process started byxdg-open
focuses the existing instance and exits.xdg-open
has no control over that, unless there's a way to make the program keep a running process. -
Krets almost 7 years
detach
is not in my distro either; but python can handle opening URLs and detaching:python -m webbrowser -t "http://example.com"
. This should work out-of-the box on almost all somewhat-recent distros of linux. -
rien333 almost 6 yearsAlthough
detach
is not even in the AUR, it proved easy to install from source and furthermore was the only program that achieved what I wanted. (the alternatives listed here and on thedetach
website don't allow closing the shell as long asxdg-open
is running, or at leastdetach
did not make my shell complain one bit) -
G-Man Says 'Reinstate Monica' about 5 yearsWhat, you believe that a function that calls another function is easier to read and understand than a single function that’s three lines long?
-
G-Man Says 'Reinstate Monica' about 5 yearsIs there some reason for this to be
"$*"
? Normally I would expect"$@"
. Now it looks like (today’s incarnation of)xdg-open
takes only one parameter, but then why not use"$1"
? But, of course, both"$*"
and"$1"
will pass an empty argument to the program if the function is invoked with no args. It really seems like this should use"$@"
. -
LondonRob about 5 years
disown
doesn't prevent errors being sent to the terminal. -
cheshirekow almost 5 years@Gilles I think you misunderstood Charl Botha. Your answer states "xdg-open waits for the program to finish. This is by design.". But then your comment says "xdg-open returns immediately"... which are contradictory.