Background a Process

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 takes up space that could ordinarily be utilized for something else. Lauching programs from the terminal can be put in the background easily with a bash script.

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

#!/bin/bash
# bgcmd – background any terminal command
# add ‘complete -cf bgcmd’ to ~/.bashrc for command completion

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

Then in the terminal use the bgcmd command with whatever application you’d like to background:

Backgrounding Already Running Processes

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 though that if the terminal or tab is closed the program will close with it. Also too the bg command doesn’t always suppress output very well.

About Gen2ly

<3's linux

9 thoughts on “Background a Process

  1. gregf says:

    nice tip

  2. patrick says:

    Hi Dirk,

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

    Cheers,
    Patrick.

  3. Dirk Gently says:

    Thanks for the detail, I added it to the post.

  4. numerodix says:

    > 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). :)

  5. numerodix says:

    Guess I’m too late…

  6. Peter K says:

    Thanks!

  7. human mathematics says:

    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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s