How to run a command in the background and get no output?

Shell

Shell Problem Overview


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.

Shell Solutions


Solution 1 - Shell

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 &

Solution 2 - Shell

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.

Solution 3 - Shell

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 &

Solution 4 - Shell

Run in a subshell to remove notifications and close STDOUT and STDERR:

(&>/dev/null script.sh &)

Solution 5 - Shell

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.

Solution 6 - Shell

nohup sh -x runShellScripts.sh &

Solution 7 - Shell

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 &

Solution 8 - Shell

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBehnam SafariView Question on Stackoverflow
Solution 1 - ShellGeorgeView Answer on Stackoverflow
Solution 2 - ShellAdam ZalcmanView Answer on Stackoverflow
Solution 3 - ShellDanteAlighieriView Answer on Stackoverflow
Solution 4 - ShellTom HaleView Answer on Stackoverflow
Solution 5 - ShellMichael ChinenView Answer on Stackoverflow
Solution 6 - Shellvaquar khanView Answer on Stackoverflow
Solution 7 - ShellSARATH CHANDRANView Answer on Stackoverflow
Solution 8 - ShellRamin IsmayilliView Answer on Stackoverflow