Having a sh script put itself into the background?

Having a sh script put itself into the background?

Post by Bryan Horli » Mon, 19 Dec 1994 04:45:46



Is there any way to have a shell script immediately put itself into the
background?  The equivalent of starting it with a & or hiting ^Z and bg.
Thanks.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

       -Head Consultant-       |          Trinity College
  "Do not use GAK on varnished |          300 Summit St
    on unvarnished surfaces"   |          Hartford, CT
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
                http://shakti.trincoll.edu/~bhorling

 
 
 

Having a sh script put itself into the background?

Post by pstu.. » Tue, 20 Dec 1994 01:13:54



Quote:>Is there any way to have a shell script immediately put itself into the
>background?  The equivalent of starting it with a & or hiting ^Z and bg.
>Thanks.

You can use the batch command and redirect stdin as follows.  If you do
not have a batch command, use at in a similar fashion.

Example
#!/bin/sh
#Program: Run me in batch
batch <<END_OF_PROGRAM
program line 1
..
last line of program
END_OF_PROGRAM

 
 
 

Having a sh script put itself into the background?

Post by Lukasz Senat » Tue, 20 Dec 1994 23:26:22




> >Is there any way to have a shell script immediately put itself into the
> >background?  The equivalent of starting it with a & or hiting ^Z and bg.
> >Thanks.

> You can use the batch command and redirect stdin as follows.  If you do
> not have a batch command, use at in a similar fashion.

This may not start the script immediately. batch or at will just queue it
for execution. Besides, you might find these commands disabled on some
systems.
A better (and simpler, IMHO) method is:

#!/bin/sh
(
program line 1
...
last line of program
) &

Quote:> Example
> #!/bin/sh
> #Program: Run me in batch
> batch <<END_OF_PROGRAM

          ^^^^^^^^^^^^^^-- you should quote it as "END_OF_PROGRAM", otherwise
environment variable expansion will happen too early. Compare two example
scripts below:

Quote:> program line 1
> ..
> last line of program
> END_OF_PROGRAM

#!/bin/sh
#example 1
X=oldval
sh << EOF
X=newval
echo $X  # will print "oldval"
EOF

#!/bin/sh
#example 2
X=oldval
sh << "EOF"
X=newval
echo $X  # will print "newval", which is what you would usually want
EOF

 
 
 

Having a sh script put itself into the background?

Post by jonathan ro » Tue, 20 Dec 1994 13:40:30



>Is there any way to have a shell script immediately put itself into the
>background?  The equivalent of starting it with a & or hiting ^Z and bg.
>Thanks.

If you trust the environment to not contain a
certain flag variable, you can do this:

        #!/bin/sh

        if test -z "$MYSCRIPT_IS_RUNNING"; then
                MYSCRIPT_IS_RUNNING=1
                export MYSCRIPT_IS_RUNNING

                exit
        fi

        #  rest of myscript goes here


                    To see a secret message, print this
              .signature at 300dpi and hold it over a candle.

 
 
 

Having a sh script put itself into the background?

Post by Ruurd Beerst » Thu, 22 Dec 1994 02:14:30



: >Is there any way to have a shell script immediately put itself into the
: >background?  The equivalent of starting it with a & or hiting ^Z and bg.
: >Thanks.

: If you trust the environment to not contain a
: certain flag variable, you can do this:

:       #!/bin/sh

:       if test -z "$MYSCRIPT_IS_RUNNING"; then
:               MYSCRIPT_IS_RUNNING=1
:               export MYSCRIPT_IS_RUNNING

:               exit
:       fi

:       #  rest of myscript goes here

Overly complicated.
Two simple solutions:

1) A simple wrapper script that calls the real script (ugly,
   uses two files).
2) #!/bin/sh
   if [ -t 0 ]
   then
      echo Backgrounding

      exit 0
   fi
   <rest of script goes here>

   This works because '&' als redirects STDIN of the backgrounded
   process to /dev/null, preventing the background process from
   reading the terminal (which the current foreground process
   is ALSO doing - not a nice thing to see).
   The "if [ -t 0 ]" tests whether the current STDIN is connected
   to a terminal. If so, it is not in the background, so it

   unchanged. The parent then exits.

Hope this helps,
        Ruurd
--
========================================================================
Ruurd Beerstra, CMG-IT, Netherlands. | #include <stddisclaimer.h>

                                     | what is he doing on my hard disk?

 
 
 

1. script that puts itself in the background

Let's say I want to do
$ ./program.sh &
But all I can do is
$ ./program.sh
The "&" I must do somehow inside the script itself.

Inside the script, all I can think of is wrapping the contents in
sh<<\EOF&
EOF
Is there a better way?  Using exec while counting SHLVL?

Must I make two files, ./first calls ./second&?

2. history files

3. Execute a sh script under perl and sh: sh script; perl script?

4. Problem compiling (linking) many KDE programs - QMultiLineEdit::resizeEvent

5. How can a Unix process put itself in background?

6. ppp problem: Config-Requests timeout

7. make C prog put itself in background???

8. More Netscape errors.

9. HELP!!! RPC server puts itself into the background.

10. put a running program in background in the program itself

11. How to get a ksh script to background itself?

12. Can a bourne shell script "background" itself