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(debugging for instance), at other times typing a program in the terminal just takes up space that could ordinarily be utilized for something else. Processes can be put in the background fairly easy using a bash script.

Sending a process to the background can be done with the nohup command (to prevent hangups) and then tell the command to suppress output by printing to /dev/null. Here’a s script for this since I find that I use it enough.

vim ~/.bin/bgcmd

Then enter the script:

#!/bin/bash
# bgcmd
# background any given command so it doesn't hang terminal

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

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

Update: Script fix, unnecessary to type quotes as seen in pic.

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.

6 Comments »

  1. gregf said

    nice tip

  2. patrick said

    Hi Dirk,

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

    Cheers,
    Patrick.

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

  4. numerodix said

    > 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 said

    Guess I’m too late…

  6. :)

RSS feed for comments on this post · TrackBack URI

Leave a Comment