How do I run an application with arguments from the command line without losing the terminal? [duplicate]
Solution 1:
Short Answer
In the unresponsive terminal:
- Hit Ctrl+Z.
- Type
bg
and enter. - Type
disown
and enter.
Long Answer
In the unresponsive terminal, hit Ctrl+Z, this will "pause" the process (or "job") and return the console control to you. However, you'll notice that gedit
becomes unresponsive and you can't use it.
Extra: if you want to, you can execute the command
jobs
, you'll notice that it'll read Stopped for thegedit
command, that's why you can't use it.
To make the job successfully run in the background (i.e. to make gedit
responsive again), execute the command bg
(meaning background). You'll now be able to use gedit
, and at the same time have the prompt to yourself.
Extra: now, if you execute
jobs
, you'll notice that it'll read Running.
You can overcome all of this from the very beginning. When you're launching gedit
from the terminal, add an &
to the end of the command, so something like this gedit /path/to/file &
. This will launch gedit
in the background from the first place (you might need to hit Enter a couple of times to get the console control back).
Extra: if you were following these extra notes, you might have noticed that the second time you did
jobs
, you could see that bash added a&
to the end of thegedit
command.
Once you get used to this system, you might notice that if you close the terminal, gedit will also terminate, without even a confirmation dialog. To prevent this from happening, run disown
, which will detach the gedit process from the terminal, removing it from the list returned by jobs
.
Solution 2:
Just type:
gedit <filename-to-edit> &
This will immediately return the command prompt to you.
Solution 3:
You could use the nohup
to prevent the GUI to be attached to a terminal:
nohup mupdf some.pdf &
This will allow you to close the terminal you are launching from, without the program being closed.
You should also notice, that the nohup command will create a file with the stdout
and stderr
of the command you run. If you want to prevent that, add &>/dev/null
before the &
.
nohup mupdf some.pdf &>/dev/null &
Solution 4:
You can also use disown
command. It is particularly useful when you've already started the process you no longer want attached to the terminal.
The basic procedure if I remember correctly, is something like this:
$ > firefox #Oops
Ctrl + z #Suspend the process
$ > bg #Push the process to the background
$ > disown #Detach most recent process started or stopped
$ > exit #Terminal gone!
Note that disown is bash
specific.
This blog post explains both methods pretty well.
Man page for disown
Solution 5:
From man gedit
:
-b, --background Run gedit in the background.
So, if you run gedit
with -b
option, it will start in background:
gedit -b [FILE-NAME]
Moreover, next you can create an alias for gedit -b
(see here how to create a permanent alias):
alias gedit='gedit -b'
From now, in the future you can use gedit [FILE-NAME]
as normal and it will start in background.