home

Executing a UNIX command in background

March 25, 2010 · 5 comments

Here’s a small UNIX tip (or how-to) for executing a command in the background. For UNIX gurus, this tip might seem like a basic one. But for guys like me who are coming from a Windows background, even smaller ones makes a big difference.

This is the scenario – I use Putty to login to UNIX. I execute few (internal) UNIX commands which are time thirtsy and take at least 2 hours to complete. While the command is running, I can’t do anything in the Putty terminal. All I can do is stare the Putty for 2 hours or do the below, to reuse the Putty session for other tasks while running the time-thirsty command in the background.

  • In UNIX shell, type your command and hit Enter.
    $ my-long-running-command
    ...... the long-running command running .....
    
  • Your command will start running. Now press Ctrl + z. This will interrupt the current process and pauses it. You’ll see something like this;
    [1]+ Stopped      my-long-running-command
    
  • Now , type bg and hit Enter. Your current process (i.e. the command) will start running in the background. To check, type jobs in the command line and hit Enter. You’ll see the job status something like this.
    $ bg
    [1]+ my-long-running-command
    $jobs
    [1]- Running my-long-running-command
    

So, now you can use Putty for other tasks, while your command executes in the background.

Related Posts

{ 5 comments… read them below or add one }

Lobo March 25, 2010 at 3:47 PM

have you tried & to send a job to background? I have never used Putty.
$sleep 15 &
[1] 1234
$sleep 10 &
[2] 5678
$fg 5678
sleep 10

Reply

Veera March 25, 2010 at 4:11 PM

cool. that’s a useful shortcut. it works in Putty too.

Reply

Raghavan alias Saravanan M November 17, 2010 at 5:30 PM

typically appending an & to the end of the command would do! :)

However it is a diff way you had suggested!

Reply

Veera November 17, 2010 at 5:40 PM

yes. I’m finding & method more quick and handy.

Reply

Raghavan alias Saravanan November 17, 2010 at 11:33 PM

Thats great :) Glad to hear that!!

Reply

Leave a Comment

Previous post:

Next post: