Background:
You can, only, bring a process/program to the foreground in the same terminal in which it was initially sent to the background ... What you're asking about, on the other hand, is to detach and transfer a running process/program from its controlling terminal and attach it to a new terminal ... That is a totally different matter and it's not easy(for a normal user) ... It gets even harder when that process has children(sub processes under it) ... The more the children the harder and more complicated it gets.
If your main process is started with a script file then you might want to get its PID this way:
pgrep -f "ScriptFileName.sh"
changing ScriptFileName.sh
to your script's actual filename.
Methods to consider:
First method
There is no guaranteed out of the box easy way to do this but, there are tools like reptyr(and others) that try to do that using ptrace ... reptyr
is available in the Ubuntu repositories so you can install it like so:
sudo apt install reptyr
and try if it works for you ... It can be used like so:
reptyr PID
or like so:
reptyr -T PID
You need, however, to either change ptrace_scope from 1
(default but, will prevent reptyr
from attaching processes) to 0
(Will revert back to 1
after reboot or you can set it back to 1
after you have finished for optimum security) for reptyr
to work like so:
echo "0" | sudo tee /proc/sys/kernel/yama/ptrace_scope
or use sudo
like so:
sudo reptyr PID
Second method
There are also ways of creating a named pipe and using GDB to attach it to the running process's STDIN or STDOUT in order to be able to write(send arguments) to or read(see the output) from that process but, this is, however a bit complicated and might not work as expected from the first time(GDB is a source-level debugger and not a regular user program) ... The process of doing this, however, looks something like this(just for demonstration ... You will face issues ... figure them out yourself):
Create a named pipe for process's STDIN like so:
mkfifo /tmp/in_pipe
Create a named pipe for process's STDOUT like so:
mkfifo /tmp/out_pipe
Attache the process like so:
sudo gdb -p PID
Close the current process's STDIN like so:
call close(0)
Reopen the process's STDIN and attach it to the named pipe like so(GDB might hang on this until the pipe is first used/written to, but follow the two steps below):
call open("/tmp/in_pipe", 0)
From another terminal, write to the named pipe arguments you want to pass to the running process like so:
echo "arg1 arg2 ..." > /tmp/in_pipe
Notice, you might need to go back to GDB terminal and continue the process if it appears stopped like so:
continue
Close the current process's STDOUT like so:
call close(1)
Reopen the process's STDOUT and attach it to the named pipe like so(GDB might hang on this until the pipe is first used/read from, but follow the two steps below):
call open("/tmp/out_pipe", 1)
From another terminal, read the output of the running process from the named pipe like so:
cat /tmp/out_pipe
Notice, you might need to go back again to GDB terminal and continue the process if it appears stopped like so:
continue
Third method
The file descriptors in a process's controlling terminal are available under /proc/PID/fd/
and you can send arguments to your script/process through /proc/PID/fd/0
with somthing like:
echo "arg1 arg2" > /proc/PID/fd/0
only if you chose to run your script/process to read from a pipe(otherwise the arguments will only be sent to the STDIN of the controlling terminal but, will not be forwarded to the script/process) initially with something like:
cat | scriptfile
You can also read the process's STDOUT from /proc/PID/fd/1
with something like:
cat /proc/PID/fd/1
Fourth method(tracing only)
There is also strace which you can use to trace the process like so:
sudo strace -p PID
or just to read the process's output like so:
sudo strace -p PID -e write
screen
to start it, so that you can switch to the screen with the server anytime – Esther Jul 21 '22 at 14:56