Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Sending command to background then exiting terminal

  1. #1

    Default Sending command to background then exiting terminal

    https://unix.stackexchange.com/questions/484276/do-disown-h-and-nohup-work-effectively-the-same

    I need to send a command to the background then exit the terminal. This is the method I've found. This command sends output to the terminal and various other commands. The specifics don't matter.

    Code:
    scanvirus -l hus > /dev/null 2>&1 &
    disown -h
    exit
    Also, I found a command called, 'nohup'. First, is this command sequence right?

    Second, how might do this from within the script? The script uses clamscan then writes out to log file. So, adding those commands directly will be hard. (I'll save the details for after the answer to the first question).

    Unless, I can have the script call itself, not true recursion.







  2. #2

    Default Re: Sending command to background then exiting terminal

    I also found a command called 'screen'. Can someone explain the differences between all these functions, more than the help file?

    https://www.computerhope.com/unix/screen.htm

    https://www.computerhope.com/unix/unohup.htm

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    27,155

    Default Re: Sending command to background then exiting terminal

    You might be confused by the description of the screen command. You should read that as coming from an age where there was no GUI at all. The user uses a text terminal (VT100 or compatible) and nothing else.

    What they call "windows" there are not the windows you know of a GUI. They are the different sessions one has open in the one terminal.

    When you have GUI it is not realy useful. In the GUI you can start as many terminal emulators as you want, and even some of them, like konsole, have the possibilty to have more tabs (more sessions) in one running emulator, so plenty of possibilites of having more text session (being shell or otherwise) running side by side.
    Henk van Velden

  4. #4
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    20,844
    Blog Entries
    14

    Default Re: Sending command to background then exiting terminal

    You can use nohup, but I'd run the entire script with it. I.e.
    Code:
    nohup script_name_here
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  5. #5
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    3,375

    Cool Re: Sending command to background then exiting terminal

    @lord_valarian:

    UNIX® «and therefore, also Linux» user processes – spawning processes – job control – doing all this from a user's CLI shell – as opposed to doing it via system library calls within an application …

    Now we need to examine what's going on here within the shell's environment, regardless of the shell being “bash” or “csh” or “ksh” or “Bourne”:
    1. “&” is a both a “metacharacter” and a “control operator” – it's context sensitive …
    2. In the job control context, appending “&” to a command will cause that command to begin executing in background – but, still attached to the originating CLI process.
    3. “disown” removes the backgrounded process from the originating shell process's job list but, the backgrounded process is still connected to the terminal where the shell is being executed – if the terminal dies, when the backgrounded process attempts to read from standard input or write to standard output, it will also die. {commit suicide … }
    4. “nohup” separates the backgrounded process from the terminal where the shell is being executed – but, the backgrounded process is not removed from the originating shell process's job list.

    All of which is not so wonderful for that which you are attempting to achieve …

    Please consider using “setsid --fork” to create a new process in a new session.

  6. #6

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by Knurpht View Post
    You can use nohup, but I'd run the entire script with it. I.e.
    Code:
    nohup script_name_here
    The user can do that. However, it needs to be automatic from within the script.

    NOTE: 'command not found error'
    Code:
    nohup linux_scan -l "$2" "$3"  > /dev/null 2>&1 &
    This is the first attempt to get this to work:

    Code:
              if [[ "$2" == *"s"* ]] || [[ "$2" == *"p"* ]]; then
                   linux_scan -l "$2" "$3"  > /dev/null 2>&1 &
                   disown -h
                   read -p "Done. Press any key..." -n1 -s;printf "";
                   kill -9 $(pgrep konsole)
              else
                   linux_scan -l "$2" "$3"
              fi
    This works with the rest of the code, but it's not done properly.

    https://askubuntu.com/questions/1539...-child-process

    Within superuser mode, I need to go up 2+ levels to get the pid of the konsole terminal.

    kconsole --> su --> superuser mode

    Code:
    #cat /proc/$PPID/comm
    su
    This is not the right level above it, parent function.

    Code:
    #pstree -hp | grep konsole

  7. #7

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by dcurtisfra View Post
    @lord_valarian:

    UNIX® «and therefore, also Linux» user processes – spawning processes – job control – doing all this from a user's CLI shell – as opposed to doing it via system library calls within an application …

    Now we need to examine what's going on here within the shell's environment, regardless of the shell being “bash” or “csh” or “ksh” or “Bourne”:
    1. “&” is a both a “metacharacter” and a “control operator” – it's context sensitive …
    2. In the job control context, appending “&” to a command will cause that command to begin executing in background – but, still attached to the originating CLI process.
    3. “disown” removes the backgrounded process from the originating shell process's job list but, the backgrounded process is still connected to the terminal where the shell is being executed – if the terminal dies, when the backgrounded process attempts to read from standard input or write to standard output, it will also die. {commit suicide … }
    4. “nohup” separates the backgrounded process from the terminal where the shell is being executed – but, the backgrounded process is not removed from the originating shell process's job list.

    All of which is not so wonderful for that which you are attempting to achieve …

    Please consider using “setsid --fork” to create a new process in a new session.
    Thanks, that is very useful information. I need to read that many more times....

    hcvv





    Re: Sending command to background then exiting terminal
    You might be confused by the description of the screen command. You should read that as coming from an age where there was no GUI at all. The user uses a text terminal (VT100 or compatible) and nothing else.

    What they call "windows" there are not the windows you know of a GUI. They are the different sessions one has open in the one terminal.

    When you have GUI it is not realy useful. In the GUI you can start as many terminal emulators as you want, and even some of them, like konsole, have the possibilty to have more tabs (more sessions) in one running emulator, so plenty of possibilites of having more text session (being shell or otherwise) running side by side.
    Henk van Velden

    If I kill the parent window, the user might have two normal tabs and a superuser user tab. I hadn't thought about that. I need think on it more.

    The user can do 'exit' and 'exit' to close the window. It saves me more coding. Hmmm...

    A simple solution, a config file option to close the window or not close it on suspend or power off.

  8. #8

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by lord_valarian View Post
    Thanks, that is very useful information. I need to read that many more times....



    If I kill the parent window, the user might have two normal tabs and a superuser user tab. I hadn't thought about that. I need think on it more.

    The user can do 'exit' and 'exit' to close the window. It saves me more coding. Hmmm...

    A simple solution, a config file option to close the window or not close it on suspend or power off.

    I need to read this material more. For now, thanks to all.

    I need to divide this into more specific questions.

  9. #9
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    3,375

    Talking Re: Sending command to background then exiting terminal

    @lord_valarian:

    “Linux System Programming” – Robert Love – ISBN-13: 978-0-596-00958-8
    Chapter 5: “Process Management” – Section: “Daemons”.

    A program (real C code, not a script) performs the following steps to become a daemon:
    1. Call fork().
    2. In the parent, call exit().
    3. Call setsid().
    4. Either, change the working directory to the root directory (/) via chdir() or, use the current working directory.
    5. Close all file descriptors.
    6. Open file descriptors 0, 1, and 2 and redirect them to /dev/null.

    The UNIX® SysV procedure had a few more steps; the current Linux systemd procedure has a few other things to do – see “man 7 daemon” …

    The CLI command “setsid” executes enough of the steps required to become a daemon in the context of shell scripts – in other words, for a shell script, it is sufficient …

  10. #10
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    27,155

    Default Re: Sending command to background then exiting terminal

    I have read the thread title ONLY.

    Doesn't the at command work for you (and yes, users must be allowed to use it by the system manager (root)).
    Henk van Velden

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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