Advertisement
Advertisement


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


Question

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.

2020/08/14
1
115
8/14/2020 1:39:32 PM

Accepted Answer

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 &
2013/12/23
226
12/23/2013 1:57:53 PM

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.

2012/02/08

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 &
2015/04/09

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.

2012/02/08

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

(&>/dev/null script.sh &)
2018/06/29

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 &
2018/08/14

Source: https://stackoverflow.com/questions/9190151
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]