I wrote two shell scripts a.sh
and b.sh
. In a.sh
and b.sh
I have a infinite for loop and they print some output to the terminal. I want to write another script which calls both a.sh
and b.sh
but I want the user to regain control of the terminal immediately, instead of having the script run infinitely and I want to hide the output in terminal.

- 1
- 11
- 47
- 78

- 2,941
- 6
- 27
- 42
8 Answers
Use nohup
if your background job takes a long time to finish or you just use SecureCRT or something like it login the server.
Redirect the stdout and stderr to /dev/null
to ignore the output.
nohup /path/to/your/script.sh > /dev/null 2>&1 &

- 4,029
- 2
- 22
- 26
-
Just to add, sometimes there are arguments for the scripts then above command will return **Ambiguous output redirect**. – sabertooth1990 Nov 06 '13 at 15:00
-
A better way that I found is to use `nohup /path/to/your/script.sh parameter1 > & 1 &` If anyone knows a better way. – sabertooth1990 Nov 06 '13 at 15:06
-
30You don't need `/dev/null 2>&1` if you're using `nohup` as it already appends all STDOUT and STDERR streams to the nohup.out file automagically. Use just: `nohup /path/to/your/command &` – leonardo Nov 13 '15 at 16:28
-
1@sabertooth1990 Your command line is faulty: `-bash: syntax error near unexpected token '&'` – notes-jj Feb 01 '17 at 17:28
-
1How would you then kill the created process? – Alexis_A May 09 '18 at 15:58
-
@Alexis_A do a `ps -ef | grep PROCESS-NAME` to find the PID then kill that. – Ebsan Feb 22 '19 at 14:41
-
This still prints `[1] + 54765 done process_name` is it possible to prevent it? – alper Jul 30 '20 at 20:48
-
@alper why? This is printed to stderr and nohup already do the redirection. If you really want to run it in background, maybe you should use a init. – Braiam Aug 14 '20 at 13:40
-
`init` how? for example following scripts does not give any output `(&>/dev/null script.sh &)` – alper Aug 14 '20 at 19:32
-
What if i need to wait until the command is done before continue script? How to just omit messages? – Max Green Mar 23 '23 at 10:48
Redirect the output to a file like this:
./a.sh > somefile 2>&1 &
This will redirect both stdout and stderr to the same file. If you want to redirect stdout and stderr to two different files use this:
./a.sh > stdoutfile 2> stderrfile &
You can use /dev/null
as one or both of the files if you don't care about the stdout and/or stderr.
See bash manpage for details about redirections.

- 26,643
- 4
- 71
- 92
Sorry this is a bit late but found the ideal solution for somple commands where you don't want any standard or error output (credit where it's due: http://felixmilea.com/2014/12/running-bash-commands-background-properly/)
This redirects output to null and keeps screen clear:
command &>/dev/null &

- 530
- 6
- 5
-
6
-
2For anyone looking at this in future who wants a solution to keep commands running after a session ends, I would suggest screen or tmux. They essential run a 'session within a session' which carries on after you close the initial session. You can then reconnect to the ongoing session if you log back in. Great for running rsync backups for example where the sync needs to run for a long time and you don't want to leave your session running. – DanteAlighieri Mar 16 '17 at 13:13
-
Also please resist using `&>/dev/null` which appears to be a shorthand of `>/dev/null 2>&1`! It might not be harmful in this example, but to clear things up: It's not POSIX compliant and always produces an exit code of 0, when using `#!/usr/bin/env sh` as a shebang on some systems. – Martin Braun Feb 15 '23 at 19:20
Run in a subshell to remove notifications and close STDOUT and STDERR:
(&>/dev/null script.sh &)

- 40,825
- 36
- 187
- 242
-
5This is the only answer that also does not print background job logs as `[1] + 54765 done
` thank you – alper Jul 30 '20 at 20:49 -
That's the only solution that worked for me too so that the output is not printed in the terminal. – Falco Nov 12 '20 at 15:57
-
What if I want to keep output to existing log files, just not in the terminal? Remove the /dev/null part? – TechFanDan Mar 13 '21 at 19:38
-
2This worked like an absolute charm! Here are some more docs on subshells: https://tldp.org/LDP/abs/html/subshells.html – cdrini May 25 '21 at 01:14
If they are in the same directory as your script that contains:
./a.sh > /dev/null 2>&1 &
./b.sh > /dev/null 2>&1 &
The &
at the end is what makes your script run in the background.
The > /dev/null 2>&1
part is not necessary - it redirects the stdout and stderr streams so you don't have to see them on the terminal, which you may want to do for noisy scripts with lots of output.

- 17,737
- 5
- 33
- 45
If you want to run the script in a linux kickstart you have to run as below .
sh /tmp/script.sh > /dev/null 2>&1 < /dev/null &

- 29,388
- 11
- 94
- 103

- 307
- 1
- 6
- 16
These examples work fine:
nohup sh prog.sh proglog.log 2>&1 &
For loop you can use like this :
for i in {1..10}; do sh prog.sh; sleep 1; done prog.log 2>&1 &
It works in background and does not show any output.

- 55
- 8