Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Sending command to background then exiting terminal

  1. #11

    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.
    I have this and other information filed under job control. Thanks.

    I can't see the process running in 'top'. (like disown -h)

    Code:
             if [[ "$2" == *"s"* ]] || [[ "$2" == *"p"* ]]; then
                   setsid --fork mswin_scan -m "$2"  > /dev/null 2>&1 &
                   #disown -h
                   #read -p "Done. Press any key..." -n1 -s;printf "";
                   
                   #close terminal windows
                   if [[ "$CloseTerminalWindows" == '1' ]];then
                        killall konsole
                   fi
              else
                   mswin_scan -m "$2"
              fi
    Also, 'nohup' gives command not found.

    I did I test run that always takes about 1-2 mins. It's long past that value and no sign of a log update.

  2. #12
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    3,138

    Cool Re: Sending command to background then exiting terminal

    Quote Originally Posted by lord_valarian View Post
    I can't see the process running in 'top'.
    Within “top”, hit the “L” key and then, type a string which is at least part of the process name and then, hit the <Return> key.

  3. #13
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    3,138

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by lord_valarian View Post
    I did I test run that always takes about 1-2 mins. It's long past that value and no sign of a log update.
    You may well have to drop the “mswin_scan -m "$2" > /dev/null 2>&1” into a script and then use “setsid --fork” call the script.
    • There shouldn't be any need to background the “mswin_scan” command's process in the forked process – the “&” after your redirections …

  4. #14
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    3,138

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by hcvv View Post
    Doesn't the at command work for you (and yes, users must be allowed to use it by the system manager (root)).
    setsid” doesn't need any system privileges – the forked process runs with the user's rights and privileges …

  5. #15

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by dcurtisfra View Post
    Within “top”, hit the “L” key and then, type a string which is at least part of the process name and then, hit the <Return> key.
    This code works. It sends the command to background. In 'top', you can see scanvirus running. If you 'exit exit' to close the window, it will still scan then suspend the system in about 1 min. Both use the same command.

    Code:
    ./scanvirusa -l hfs bin
    linux scan - high-priority - suspend on complete - scan folder bin

    On the first section of code, 'clamscan' appears at the top and stays there until the scan completes.


    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 "";
                   
                   #lock screen
                   if [[ "$LockScreenCommand" == '1' ]];then
                        #printf "Desktop:  %s\n" $XDG_CURRENT_DESKTOP
                        if [[ $(wmctrl -m | grep KWin) ]]; then
                             loginctl lock-session
                        fi
                        if [[ $(wmctrl -m | grep GNOME) ]]; then     
                             gnome-screensaver-command --lock
                        fi
                   fi
              else
                   linux_scan -l "$2" "$3"
              fi
    The user shouldn't have to use the 'fork' command. This method is fully automatic.

    I don't fully understand what 'fork' does vs '&' and 'disown'. I'm leaning the basics.

    This is 'fork' in the code:

    Code:
              if [[ "$2" == *"s"* ]] || [[ "$2" == *"p"* ]]; then
                   setsid --fork linux_scan -l "$2" "$3"  > /dev/null 2>&1
                   #disown -h
                   #read -p "Done. Press any key..." -n1 -s;printf "";
                   
                   #lock screen
                   if [[ "$LockScreenCommand" == '1' ]];then
                        #printf "Desktop:  %s\n" $XDG_CURRENT_DESKTOP
                        if [[ $(wmctrl -m | grep KWin) ]]; then
                             loginctl lock-session
                        fi
                        if [[ $(wmctrl -m | grep GNOME) ]]; then     
                             gnome-screensaver-command --lock
                        fi
                   fi
              else
                   linux_scan -l "$2" "$3"
              fi
    I checked the 'top' command. I tried 'l'. It doesn't work. Says, "floating point # not accepted".

  6. #16

    Default Re: Sending command to background then exiting terminal

    I used '1' and 'l'. Clamscan still doesn't appear.

  7. #17

    Default Re: Sending command to background then exiting terminal

    This has turned into a different issue. I need to start a new topic line.

    Thanks to all.

  8. #18

    Default Re: Sending command to background then exiting terminal

    nohup is the way to go.

    try : nohup scanvirus -l hus > /dev/null 2>&1 &

    The scan will run background and you may close the shell or logout, it will continue executing background.

    If you want to see results : nohup scanvirus -l hus &
    The file nohup.out in the current directory will contains the output of the scanvirus.

    simple,
    BT

  9. #19

    Default Re: Sending command to background then exiting terminal

    Quote Originally Posted by PerfMonk View Post
    nohup is the way to go.

    try : nohup scanvirus -l hus > /dev/null 2>&1 &

    The scan will run background and you may close the shell or logout, it will continue executing background.

    If you want to see results : nohup scanvirus -l hus &
    The file nohup.out in the current directory will contains the output of the scanvirus.

    simple,
    BT
    This must operate within the script. This code works.

    Code:
                   linux_scan -l "$2" "$3"  > /dev/null 2>&1 &
                   disown -h

    This gives an error.

    Code:
    nohup linux_scan -l "$2" "$3" &
    nohup linux_scan -l "$2" "$3"
    nohup: appending output to 'nohup.out'
    nohup: failed to run command 'linux_scan': No such file or directory

Page 2 of 2 FirstFirst 12

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
  •