Send current script to background


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Send current script to background
# 1  
Old 03-31-2017
Send current script to background

Hi scripters,

I'm quite used to run commands in the background using & like in:
Code:
$ myscript &

But this is NOT what I'm trying to do today. What I'm trying to achieve is to run a script the normal way (without &), have my script do a little checkup and then jump to background. Something like:
Code:
#!/bin/bash
if [[ -z ${1+set} ]]; then
    echo "Missing argument"
    exit 1
fi
echo "Arguments OK, going to background"
some_command_that_sends_the_script_to_background
# At that point, the user should have its terminal back and the script should be sent to bg
while true; do
    echo "doing some never ending daemon stuff" > log
    sleep 5
done

I tried the following:
Code:
bg
disown
exec &

If I google the following strings, I get nothing but what I already know :
bash current script to background
bash script send to background
bash send to background in the middle of a script


Can anyone point me in the right direction?

Thanks
Santiago
# 2  
Old 03-31-2017
A block in parenthesis can be sent to the background
Code:
(
while true; do
    echo "doing some never ending daemon stuff" > log
    sleep 5
done
) </dev/null >/dev/null 2>&1 &

It is safer to disable stdin,stdout,stderr. E.g. the sleep command could try to set a CTRL-C interaction from the terminal.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 03-31-2017
Processes don't work that way. The way to make a controlling process not be the controlling process, is to create a background process then make the foreground one quit. Hence why you need a subshell to do this one way or another.
# 4  
Old 03-31-2017
Thanks MadeInGermany,

I'd rather not create a subshell. A lot of the scripts in which I need that feature use functions meaning I would have to replace all the `return' with some `exit'. But I see that it solves the problem somehow.

Thanks Corona688,

I don't understand what you're saying. What command are you proposing? I think `sesid' could do what I need but I can't get it to work.
# 5  
Old 03-31-2017
Quote:
Originally Posted by chebarbudo
I'd rather not create a subshell. A lot of the scripts in which I need that feature use functions meaning I would have to replace all the `return' with some `exit'.
You can't move the controlling shell into the background, just create a new one.

There's more than one way to skin a cat however, I'll try to whip up a method that changes your code less.

Quote:
Thanks Corona688,

I don't understand what you're saying. What command are you proposing?
MadeInGermany's.

It creates a background process, via subshell, then quits.

Quote:
I think `sesid' could do what I need
Processes don't work that way.
# 6  
Old 03-31-2017
How about this:

Code:
#!/bin/bash

if [[ -z ${1+set} ]]
then
        echo "Missing argument" >&2
        exit 1
fi

# Check if we're a background script
if [ -z "${_FG}" ]
then
        # Warn subshell that it is a subshell
        export _FG="1"

        # Re-run entire script and warn it that its a subshell
        ( exec "$0" "$@" ) &
        exit 0
fi

# This code will run in background
echo "Yeah okay we're in subshell"
sleep 10
echo "Woo"

When _FG is not set, it sets it, re-executes the program from scratch, then quits.

When _FG is set, it skips that and runs the code below.

This also means you can run your programs in the foreground, if you really feel like it, by doing _FG=1 ./myprogram.sh arguments
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to send a file in UNIX through email which is created only 15 minutes before the current time?

I wanted to send an email to the client whenever there is failed record created in a /feed/HR-76/failed folder after processing of feed file. I can find out with the help of below script that what is the new file created but that file didn't make just 15 minutes before. ... (1 Reply)
Discussion started by: puneetkhullar
1 Replies

2. Shell Programming and Scripting

Send sleep to background

Hello all, I've a small script where it checks for the existence of a particular file and sleeps for 5 seconds if it doesn't exist. I would like to send the sleep output to background so that I don't see so many sleep messages in the build output. #!/bin/sh -x until do sleep 3 done echo... (17 Replies)
Discussion started by: builderj
17 Replies

3. UNIX for Dummies Questions & Answers

How do I send output of a background process to a file other than nohup.out?

I have a question. I will be running a background process using nohup and & command at end. I want to send output to a file say myprocess.out. So will this command work? nohup myprocess.ksh > myprocess.out & Thanks in advance guys !!! :) (3 Replies)
Discussion started by: vx04
3 Replies

4. Shell Programming and Scripting

Perl to send previous and current value

For example, I have a file called number.txt. x y 1 1 2 4 3 9 4 6 5 5 6 6 7 9 8 4 9 1 10 0 ... And I want to print out the value of x and y, if y%4==0 and the next value of y%4==0. Thus, the sample output is: 1 1 *because the previous x before 2 is 1 2 4 *because 4%4 == 0 7 9... (2 Replies)
Discussion started by: Tzeronone
2 Replies

5. UNIX for Dummies Questions & Answers

Send job to Background after input redirection

Hi, I am having issues with syntax when I am trying to send a job to the background after a input redirection. I have this script which sends some files to different servers after zipping them. Once I execute it, it will ask for user input as of which server the files need to go to. (The... (3 Replies)
Discussion started by: grep_me
3 Replies

6. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

7. Shell Programming and Scripting

Send Foreground job to background redirecting output

I have many CPU intensive processes running and sometimes I run them in the foreground so that I can see what the output is. I want to send that foreground process to the background, but also have it direct the output to a logfile. I know to send something to the bg I do Ctrl-z on the FG... (6 Replies)
Discussion started by: jhullbuzz
6 Replies

8. UNIX for Advanced & Expert Users

send a new value to a variable in a running background process

Hi guys, I have a issue with a background process, I need to update the value of a variable in that process which is running at this time and it will be running for at least 2 days. Any idea? I will apreciate your help. regards. Razziel. (2 Replies)
Discussion started by: razziel
2 Replies

9. Shell Programming and Scripting

Send password : In Background

Hi All, How to pass the password in background.....? " /usr/bin/hdiutil attach -mountroot -stdinpass /path/to/mount/ /path/of/image/TEST.sparseimage " The above command asks the password to mount a sparse image and when supplied it gets mount. But my requirement is to use... (1 Reply)
Discussion started by: ashwin.patil
1 Replies

10. UNIX for Advanced & Expert Users

send characters to current window

Before I re-invent the wheel... I have written an on-screen keyboard & handwriting input client in Java (spare me please, I get paid to write Java and it will take some time to get back up to speed in C & X11). In order to concentrate on the other bits, I took advantage of a hack... (0 Replies)
Discussion started by: bcw
0 Replies
Login or Register to Ask a Question