Put currently running script into background

Hi All,
Suppose I have a script and inside it I want/need to put it into background. I need the script to not react to SIGHUP signals.
I tried:

#!/bin/bash
echo "" > test_disown
mypid=$$
echo "PID=$mypid"
(
kill -SIGSTOP $mypid
jobs > myjobs
#disown -h <job-spec>
#kill -SIGCONT $mypid
) &
while [ 1 ]; do
        echo $(date) >> test_disown
        sleep 10
done

When I run this script I have the output:

'disown' test
PID=7083
 [4]+  Stopped                 ./test.sh

Now from the console I can do "bg %4" and the script runs in background.
I want to run bg from the script, but I don't know the job-spec number of my stopped script.
The line "jobs > myjobs" created empty file myjobs.
How to overcome this?

Please help,
Jacek

If you are in a login shell and do not want to send a SIGHUP signal to children running when you exit that shell, use disown to avoid sending SIGHUP signals to the processes listed as operands in a disown command.

If you are a child who doesn't want to act on a SIGHUP sent by a login shell when it dies, use trap with a null action to ignore SIGHUP signals:

trap '' SIGHUP

Thanks Don for your answer.
I don't know if you understand me correctly...
I have script test.sh:

#!/bin/bash
# Script-name: test.sh
echo "'disown' test"
echo "" > test.out
#disown -h $$
mypid=$$
echo "PID=$mypid"
(
kill -SIGSTOP $mypid > test_stop_out 2>&1
jobs > myjobs
#kill -SIGCONT $mypid
) &
while [ 1 ]; do
        echo $(date) >> test_out
        sleep 10
done

I want to put this script into background. I don't want to use Ctrl+Z for this, so I execute a commands in subshell:

kill -SIGSTOP $mypid > test_stop_out 2>&1
jobs > myjobs

The kill stopped my script but after this I need to execute bg to put the script into background. I don't know how to get job-spec number of my stopped script: test_stop_out and myjobs files are empty :confused:

Not sure I understand why you "want to put this script into background". Either it already IS running standalone (so background is not needed), or you are in an interactive session and thus are in a position to enter <CTRL>-Z.

First note that a stopped (or running process) cannot affect the list of background jobs known by a parent shell (which is what your script is trying to do). And no shell can become a background job of itself (which is something else that your script is trying to do).

What is it that you had hoped the script:

#!/bin/bash
# Script-name: test.sh
echo "'disown' test"
echo "" > test.out
#disown -h $$
mypid=$$
echo "PID=$mypid"
(
kill -SIGSTOP $mypid > test_stop_out 2>&1
jobs > myjobs
#kill -SIGCONT $mypid
) &
while [ 1 ]; do
        echo $(date) >> test_out
        sleep 10
done

would do that is not done by the script:

#!/bin/bash
# Script-name: test2.sh
trap '' SIGHUP
printf "'disown' test2\n\n" > test2.out

mypid=$$
echo "PID=$mypid"

while [ 1 ]
do	date >> test2.out
	sleep 10
done

other than the fact that all output is written by this script to the file test2.out instead of writing some output to test.out and appending other output to test_out ?

Hi,
I think I can do it simply by creating two scripts: test.sh and test_bg.sh
test_bg.sh will be executed in test.sh by the command:

nohup test_bg.sh &

I've got a question now... How may I bring test_bg.sh to the foreground (it will be running with PPID=1)?

Hi JackK,
I assume that sometime in the last four days you tried what I suggested in post #5 in this thread and decided that it won't work. Please explain what it did that you didn't want it to do or that it didn't do that you wanted it to do!

Only the parent shell of a process placed in the background can bring that child back into the foreground. If the parent exits after backgrounding a child, that child cannot be brought back to the foreground by another process.

If the child knew what its paren't controlling terminal was when it was backgrounded and the terminal session is still active, a child might be able to reconnect itself to that terminal session, but until you clearly explain what you are trying to do I will not spend any time trying to research whether or not this is possible in a shell script.