Advertisement: Linux VPS from $4/month - contact support for custom offer.
+ Post New Thread
Results 1 to 3 of 3

Thread: How to run linux command on background in bash(shell) script ?

  1. #1
    Administrator
    Join Date
    Mar 2013
    Posts
    2,802

    How to run linux command on background in bash(shell) script ?

    Hello, i just want to share this bash script code.

    # definning emptybuffer function which will sleep 3600 seconds and empty buffer file when called.
    # these commands (sleep, >) will be executed on background and script can continue to run
    emptybuffer(){
    sleep 3600
    >/root/script/buffer

    }

    # if there is no "sleep 3600" process running, then we execute it (function emptybuffer)
    if [ "$(ps x | grep "sleep 3600" | wc -l)" -lt "2" ];then
    emptybuffer &
    fi
    Please if you know any other ways to simply execute linux command from bash script on background, please kindly share, thx

  2. #2
    Junior Member imort's Avatar
    Join Date
    Apr 2016
    Posts
    15


    Is this useful / helpfull? Yes | No
    Quote Originally Posted by admin View Post
    Hello, i just want to share this bash script code.
    Please if you know any other ways to simply execute linux command from bash script on background, please kindly share, thx
    You can just run the command like that:

    ./your_script.sh &
    and it will be executed in the background return some result and finished.

  3. #3


    Is this useful / helpfull? Yes | No
    I have one more way of achieving this. Here are the steps:

    If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.

    You can view all your background jobs by typing jobs. However, its stdin, stdout, stderr are still joined to the terminal.

    $ tar -czf home.tar.gz .
    $ bg
    $ jobs
    Run Linux Command in Background
    Run Linux Command in Background

    You can as well run a process directly from the background using the ampersand, & sign.

    $ tar -czf home.tar.gz . &
    $ jobs
    Start Linux Process in Background
    Start Linux Process in Background
    Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.

+ Post New Thread

Similar Threads

  1. Replies: 0
    Last Post: 05-31-2015, 04:16 PM
  2. Replies: 0
    Last Post: 11-16-2014, 07:42 PM
  3. Replies: 0
    Last Post: 06-20-2014, 09:56 AM
  4. How to read linux bash command output and discover if match.
    By Fli in forum Programming, scripting
    Replies: 0
    Last Post: 10-25-2013, 09:17 PM
  5. Replies: 0
    Last Post: 09-29-2013, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
 Protected by : ZB BLOCK  &  StopForumSpam