Results 1 to 5 of 5

Thread: running a program in the background in linux

  1. #1
    Join Date
    Feb 2014
    Beans
    8

    running a program in the background in linux

    I need to run a program in the background . I know that using '&' after a command runs the command in the background. In the give program:-
    Code:
    #!/bin/bash
    echo amt of time delay nsec
    read nano
    echo amt of time delay in sec
    read sec
    echo no.of times
    read i
    while [ $i -ne 0 ]
    do
    ./nanosleep $sec $nano
    ./schello
    i=$[i-1]
    done
    I have a few data that i get from the user. Is there a way I can run the program in background? i.e, while calling the program can i also specify the data required(like nano, sec,i) as arguments or through some other way?
    Last edited by aashik2; March 26th, 2014 at 01:12 PM.

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: running a program in the background in linux

    There are a few options.
    * Get the inputs from files, not interactively.
    * Call the long-running parts from a different script - that runs in the background. That would probably be the loop and everything inside it.
    * Push long running tasks onto a queue manager - like TaskSpooler
    Or
    * after the script starts, use the built-in process management inside almost every shell to stop the process and put it into the background. cntl-z, then type 'bg' - the bad part of this is that output will be sent to the terminal and will interrupt any other tasks ... including running CLI programs.
    Last edited by TheFu; March 26th, 2014 at 05:03 PM.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: running a program in the background in linux

    Quote Originally Posted by aashik2 View Post
    while calling the program can i also specify the data required(like nano, sec,i) as arguments
    Yes - if you call the script as

    Code:
    ./myscript 123 456
    then the values (123 and 456) will be available as positional parameters $1 and $2 inside your script

  4. #4
    Join Date
    Mar 2014
    Beans
    31

    Re: running a program in the background in linux

    To background a script, you can use 'nohup'. so, paraphrasing steeldriver: nohup ./myscript 123 456
    then do a "ps -ef | grep myscript" to make sure it is running.

  5. #5
    Join Date
    Mar 2014
    Location
    Greater Noida,UP,India
    Beans
    78
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: running a program in the background in linux

    Along with using & after the command,using 'nohup' will be beneficial...........and you can view that your program is successfully by typing the command 'top'.......

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •