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.

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.

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

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.

You can use nohup, but I’d run the entire script with it. I.e.


nohup script_name_here

@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 …
[HR][/HR]Please consider using “setsid --fork” to create a new process in a new session.

The user can do that. However, it needs to be automatic from within the script.

NOTE: ‘command not found error’

nohup linux_scan -l "$2" "$3"  > /dev/null 2>&1 &

This is the first attempt to get this to work:

          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.

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

kconsole → su → superuser mode

#cat /proc/$PPID/comm
su

This is not the right level above it, parent function.

#pstree -hp | grep konsole

Thanks, that is very useful information. I need to read that many more times…

                                                                                                                                    **hcvv**](https://forums.opensuse.org/member.php/180-hcvv)      
                                           **https://forums.opensuse.org/images/icons/icon1.png 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.

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

I need to divide this into more specific questions.

@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” …
[HR][/HR]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 … >:)

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)).

I have this and other information filed under job control. Thanks.

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

         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.

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.

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 …

setsid” doesn’t need any system privileges – the forked process runs with the user’s rights and privileges …

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.

./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.

          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
" $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:

          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
" $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”.

I used ‘1’ and ‘l’. Clamscan still doesn’t appear.

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

Thanks to all.

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 :cool:

This must operate within the script. This code works.

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

This gives an error.

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