BrainDump
  Home arrow BrainDump arrow Page 4 - Executing Commands with bash
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
BRAINDUMP

Executing Commands with bash
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-06-12


    Table of Contents:
  • Executing Commands with bash
  • 4.2 Telling If a Command Succeeded or Not
  • 4.3 Running Several Commands in Sequence
  • 4.4 Running Several Commands All at Once
  • 4.5 Deciding Whether a Command Succeeds

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Executing Commands with bash - 4.4 Running Several Commands All at Once
    ( Page 4 of 5 )

    Problem

    You need to run three commands, but they are independent of each other, and don’t need to wait for each other to complete.

    Solution

    You can run a command in the background by putting an ampersand (&) at the end of the command line. Thus, you could fire off all three jobs in rapid succession as follows:

      $ long &
     
    [1] 4592
      $ medium &
      [2] 4593
      $ short
      $

    Or better yet, you can do it all on one command line:

      $ long & medium & short
      [1] 4592
     
    [2] 4593
      $

    Discussion

    When we run a command in the background (there really is no such place in Linux), all that really means is that we disconnect keyboard input from the command and the shell doesn’t wait for the command to complete before it gives another prompt and accepts more command input. Output from the job (unless we take explicit action to do otherwise) will still come to the screen, so all three jobs will be interspersing output to the screen.

    The odd bits of numerical output are the job number in square brackets, followed by the process ID of the command that we just started in the background. In our example, job 1 (process 4592) is the long command, and job 2 (process 4593) is medium.

    We didn’t put short into the background since we didn’t put an ampersand at the end of the line, so bash will wait for it to complete before giving us the shell prompt (the $ ).

    The job number or process ID can be used to provide limited control over the job. You can kill the long job with kill %1 (since its job number was 1 ). Or you could specify the process number (e.g., kill 4592 ) with the same deadly results.

    You can also use the job number to reconnect to a background job. Connect it back to the foreground with fg %1 . But if you only had one job running in the background, you wouldn’t even need the job number, just fg by itself.

    If you start a job and then realize it will take longer to complete than you thought, you can pause it using Ctrl-Z, which will return you to a prompt. You can then type bg to un-pause the job so it will continue running in the background. This is basically adding a trailing & after the fact.

    See Also

    Chapter 2 on redirecting output



     
     
    >>> More BrainDump Articles          >>> More By O'Reilly Media
     

       

    BRAINDUMP ARTICLES

    - Migrating Oracle to PostgreSQL with Enterpri...
    - Demystifying SELinux on Kernel 2.6
    - Yahoo and Microsoft Create Ad Partnership
    - The Advantages of Obscure Open Source Browse...
    - Dell Announces CSI-style Digital Forensics S...
    - Milepost GCC Speeds Open-Source Development
    - Learn These 10 Programming Languages
    - Tomcat Capacity Planning
    - Internal and External Performance Tuning wit...
    - Tomcat Benchmark Procedure
    - Benchmarking Tomcat Performance
    - Tomcat Performance Tuning
    - Wubi: Windows-based Ubuntu Installer
    - Configuring and Optimizing Your I/O Scheduler
    - Linux I/O Schedulers





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek