- How to run a command in the background and get no output?
- 8 Answers 8
- How to Run Linux Commands in Background and Detach in Terminal
- Run Linux Command or Process in Background
- Keep Linux Process Running After Logout
- Detach a Linux Process From Terminal
- Related Posts
- 10 thoughts on “How to Run Linux Commands in Background and Detach in Terminal”
- executing shell command in background from script [duplicate]
- 4 Answers 4
How to run a command in the background and get no output?
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.
8 Answers 8
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 &
Just to add, sometimes there are arguments for the scripts then above command will return Ambiguous output redirect.
A better way that I found is to use nohup /path/to/your/script.sh parameter1 > & 1 & If anyone knows a better way.
You 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 &
Redirect the output to a file like this:
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.
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:
For 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.
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.
How to Run Linux Commands in Background and Detach in Terminal
In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, which is how to completely detach a process from its controlling terminal.
When a Linux process is associated with a terminal, two problems might occur:
- Your controlling terminal is filled with so much output data and error/diagnostic messages.
- In the event that the terminal is closed, the process together with its child processes will be terminated.
To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background in Linux.
Run Linux Command or Process in Background
If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.
You can view all your background jobs by typing jobs . However, its stdin, stdout, and stderr are still joined to the terminal.
$ tar -czf home.tar.gz . $ bg $ jobs
You can as well run a Linux process in the background using the ampersand, & sign.
$ tar -czf home.tar.gz . & $ jobs
Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.
$ tar -czf home.tar.gz . & $ jobs tar: ./.config/etcher: Cannot open: Permission denied
Keep Linux Process Running After Logout
We will use the disown command, which is used after the process has been executed and put in the background, its work is to remove a shell job from the shell’s active list jobs, therefore you will not use fg , bg commands on that particular job anymore.
In addition, when you close the controlling terminal or log out, the job will not hang or send a SIGHUP to any child jobs.
Let’s take a look at the below example of using the diswon bash built-in function.
$ sudo rsync Templates/* /var/www/html/files/ & $ jobs $ disown -h %1 $ jobs
You can also use the nohup command, which also enables a process to continue running in the background when a user exits a shell.
$ nohup tar -czf iso.tar.gz Templates/* & $ jobs
You might also like:
Detach a Linux Process From Terminal
Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as Firefox:
In Linux, /dev/null is a special device file that writes off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.
You might also like:
As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the controlling terminal, your process and child processes will be terminated.
Importantly, for any questions or remarks on the subject, reach us by using the comment form below.
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
10 thoughts on “How to Run Linux Commands in Background and Detach in Terminal”
Hey, Thanks for this little Linux tip, useful for me to make my Linux commands run in the background… Reply
Without the “disown” you’ll get the following output in the source terminal after the given program is closed: “[1]+ Done firefox /dev/null” This only happens the next time something is put out to the terminal’s output stream. So, the next time command runs that writes to the output stream. To test this, run it without “disown”, close firefox, and then run “ls”. Reply
I see this is an old post but I think my question is sort of in the ballpark. what I would like to do is open the console of the process that was started with systemd with a no GUI flag. what this pertains to the Minecraft server. so I made a systemd service file and the execution command is “java -jar server.jar nogui” now once the system is up and running, I would like to open the GUI or console of the Minecraft server to issue commands without having to open a Minecraft launcher like you would play the game. I know I have done something like this in the past on other platforms but I just don’t know enough about Minecraft/java to make this happen. I am using ubuntu 18.04 and the current version of the Minecraft is 1.15.2 Reply
How to see the jobs running in the previous session? I have submitted jobs in nohup and logged out. After logging again if i wanna see the jobs and if I wanna kill, how to do so Reply
@Bala You can run this command:
ps -fu username #replace username with your real username
OR
ps -x Reply
executing shell command in background from script [duplicate]
how can I execute a shell command in the background from within a bash script, if the command is in a string? For example:
#!/bin/bash cmd="nohup mycommand"; other_cmd="nohup othercommand"; "$cmd &"; "$othercmd &";
4 Answers 4
nicholas@nick-win7 /tmp $ cat test #!/bin/bash cmd="ls -la" $cmd & nicholas@nick-win7 /tmp $ ./test nicholas@nick-win7 /tmp $ total 6 drwxrwxrwt+ 1 nicholas root 0 2010-09-10 20:44 . drwxr-xr-x+ 1 nicholas root 4096 2010-09-10 14:40 .. -rwxrwxrwx 1 nicholas None 35 2010-09-10 20:44 test -rwxr-xr-x 1 nicholas None 41 2010-09-10 20:43 test~
Also make sure you do not put quotations around $cmd . If you do, it will try to run a command called «ls -la» instead of ls with the switches -la .
cmd=’some-command «with arguments»‘; $cmd is not at all the same as some-command «with arguments» . It’ll fail if you have quoting in your argument list; it’ll fail if you need to control when globs are and are not expanded; it’ll fail if running code that depends on brace expansion or parameter expansion at execution time. See BashFAQ #50: I’m trying to put a command in a variable, but complex cases always fail!.
Building off of ngoozeff ‘s answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:
- &>/dev/null sets the command’s stdout and stderr to /dev/null instead of inheriting them from the parent process.
- & makes the shell run the command in the background.
- disown removes the “current” job, last one stopped or put in the background, from under the shell’s job control.
In some shells you can also use &! instead of & disown ; they both have the same effect. Bash doesn’t support &! , though.
Also, when putting a command inside of a variable, it’s more proper to use eval «$» rather than «$» :
If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.
#!/bin/bash # Run a command in the background. _evalBg() < eval "$@" &>/dev/null & disown; > cmd="google-chrome"; _evalBg "$";