LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2010, 06:23 AM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Rep: Reputation: 34
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
 
Old 10-06-2010, 06:46 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 10-06-2010, 06:49 AM   #3
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Original Poster
Rep: Reputation: 34
Hi EricTRA, thanks for the reply
 
Old 10-06-2010, 06:51 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

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

Kind regards,

Eric
 
Old 10-06-2010, 06:56 AM   #5
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Original Poster
Rep: Reputation: 34
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
 
Old 10-06-2010, 07:07 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 10-06-2010, 11:29 PM   #7
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Original Poster
Rep: Reputation: 34
Grete EricTRA, thanks you and Best Regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script-How to call tail then exit when 'END timestamp' appears in log? Mountain Linux - Software 4 05-01-2009 03:29 PM
shell script: how to send stdout one place & stderr another? BrianK Programming 8 09-21-2007 07:57 AM
where to place certiain commands inside shell script beeblequix Linux - Newbie 8 08-10-2006 12:12 PM
A place to find good back ground pics for linux? draven66 Linux - Software 4 05-11-2004 06:20 PM
running root-tail from shell script not working Nuk Linux - Newbie 1 04-09-2004 09:56 AM


All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration