Simple answer: yes, of course you can!
How and why, I hear you say?
What if I need to run a program that does not play well with the console, e.g. the KDE file explorer, Dolphin?
When I run that from the command line and it prints out streams of internal Qt low level messages and mostly (if it's working correctly) these are meaningless and can be ignored. There are sometimes so many that they tend to make the terminal useless.
The following would be a way to stop that happening. And (if properly configured) could also be used to make it send the terminal output STDOUT
to a named pipe, connected to a TCP server by using programs like netcat or the more capable socat.
This example is great if you want to run a noisy program from your login script.
function shush () { \
QC="$1"; \
shift; \
nohup "${QC}" $* >/dev/null 2>&1 & \
disown; \
}
This make use of the bash function
, alias
and job control
features.
How this works:
- We capture the 1st command parameter into
${QC}
as that will be the command we want to run later
shift
is used to drop the 1st paramter so it does not get passed again
nohup
is used to disconnect stdin
and stdout
from the app
2>&1
is used to redirect stderr
to stdout
>/dev/null
pipes stdout
into /dev/null
to get swallowed into nothingness.
- The expansion of the string
${QC}
is the command we will run, the quotes are there just in case the path has spaces in it.
$*
expands all the remaining parameters, the same as $1 $2 $3 ....
- the ampersand
&
makes the command into a background job
disown
disassociates the last job from the terminal that started the job
nohup
would normally create a file nohup.out
, so the pipe to /dev/null prevent that.
disown
is (usually) a shell built in statement and works by making the last background command (aka job) immune to signals such as HUP coming from the terminal. That means closing the terminal will not close the program.
BTW: I use this to allow me to run multiple GUI commands in Windows under WSL!
Now we can do...
> shush dolphin
Perhaps we would want to use it in a terminal too, we don't want to keep typing shush.
Then do this in ~/.bash_aliases
alias dolphin='shush dolphin'
Now we can do both, the now shushed dolphin
command works the same in a terminal or in a script, with or without command line parameters.
Ahh, I hear you say, but what if we need to undo that and "unshush" it, e.g. to use the --help
command line option?
There are at least two ways.
Quick and dirty, just bypass the alias using which
$(which dolphin) --help
Another way would be to modify the shush ()
function/script to extract the 2nd parameter (or scan all of them) and check if it is --noshush
and/or has other parameters that need the terminal and act accordingly, but lets make that homework for the reader ;)
$?
fromdate
, not fromjava
. Gotta be careful about order-of-operations.nohup
only does any redirection at all if your program has handles on the TTY in the first place; if its handles are already on a file,nohup
leaves them alone and doesn't opennohup.out
at all.setsid
(run a program in a new session) chance. You can combine it withnohup
.