LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   place a shell script with tail -f in the back ground (https://www.linuxquestions.org/questions/linux-newbie-8/place-a-shell-script-with-tail-f-in-the-back-ground-836503/)

procfs 10-06-2010 05:23 AM

place a shell script with tail -f in the back ground
 
Following script name is 123.sh and I need to put this in the background if I do 123.sh -bg this will not bring me back to the prompt but echoes what ever I put (using echo hello >> /tmp/123) in to the /temp/123 file. the only way that I have found doing this is to do "nohup 123.sh &" to put this in to the background. Is this okay or is there any better way of doing this?

#!/bin/bash
# file name is 123.sh

tail -f /temp/123 | while read line
do

echo "Loop rane for $COUNT times"
let COUNT=$COUNT+1

done



Thank you
Best Regards

EricTRA 10-06-2010 05:46 AM

Hello,

If you run a script like this:
Code:

./scriptname.sh -bg
you're giving -bg as arguments to the script AFAIK. If your script is not written to 'understand' the arguments then they will have no effect.

You can execute the script directly with & at the end like this:
Code:

./scriptname.sh &
but that will only keep it running while your session is logged in. As soon as you disconnect from the session (console,SSH,...) the script will stop running.

You can also put a script in the background with CTRL-Z until you need the script to run again. Putting it in the background this way will stop the script.

The only way I know of to 'detach' a script from a terminal/console/session is by using nohup like you did.

Kind regards,

Eric

procfs 10-06-2010 05:49 AM

Hi EricTRA, thanks for the reply

EricTRA 10-06-2010 05:51 AM

Hi,

You're welcome! If you consider your question/problem solved then please mark it as such using the Thread Tools.

Kind regards,

Eric

procfs 10-06-2010 05:56 AM

Hi EricTRA, one more thing, how do I write -bg in to the script, any resource that I could refer to?

Thanks and Best Regards

EricTRA 10-06-2010 06:07 AM

Hello,

If you only want the loop in the background then you do it like this:
Code:

#!/bin/bash
# file name is 123.sh

tail -f /temp/123 | while read line
do

echo "Loop rane for $COUNT times"
let COUNT=$COUNT+1

done &

It all depends what you want to run in the background and what result you are expecting.

Best reading material I've encountered is in my personal opinion:
Bash Guide for Beginners
and
Advanced Bash Scripting Guide in which you can find this example.

Kind regards,

Eric

procfs 10-06-2010 10:29 PM

Grete EricTRA, thanks you and Best Regards


All times are GMT -5. The time now is 07:39 PM.