Background a process or program

At times it is useful to see the output of what a program produces by typing its command line name in the terminal (for instance for debugging). At other times typing a program in the terminal just hogs the prompt that could ordinarily be utilized for something else. Launching programs from the terminal can be put in the background easily and I’ve created a bash script to help out.

nohup is used to prevent hangups and then you redirect some the output of the command to /dev/null (the great Linux black hole). Here’s the script:

#!/usr/bin/bash
# Open a command in the terminal in the background.
# Add 'complete -cf bgcmd' to ~/.bashrc for command completion

nohup "$@" &> /dev/null &

Then in the terminal use the bgcmd command with whatever program needed to be put in the background:

bgcmd gedit ~/Documents/recipes.markdown

Backgrounding an already running process

Already running applications can be backgrounded as well. First type Ctrl + z to release the application, then use bg to background it’s output.

Keep in mind that if the terminal or tab is closed the program will close with it; also too the bg command doesn’t suppress all output.

8 thoughts on “Background a process or program

  1. patrick

    Hi Dirk,

    replace ‘$1’ (first parameter) with ‘$*’ (all parameters) and you can forget about the quoting ;)

    Cheers,
    Patrick.

    Reply
  2. numerodix

    > Keep in mind any command that has a space will need put in parenthesis.

    You can get around this. Change your script to this:

    nohup $@ &> /dev/null &

    This will capture all the arguments given to backprocess ($@), not just the first one ($1). :)

    Reply
  3. human mathematics

    Can you use bg to “sleep” processes, like if I have a lot of PDF’s open that I want to read “later” can I bg them to save memory?

    Reply

Leave a reply to human mathematics Cancel reply