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.
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.
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.
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”:
“&” is a both a “metacharacter” and a “control operator” – it’s context sensitive …
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.
“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 … }
“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.
**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.
“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:
Call fork().
In the parent, call exit().
Call setsid().
Either, change the working directory to the root directory (/) via chdir() or, use the current working directory.
Close all file descriptors.
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 … >:)
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”.
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