LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-08-2015, 04:51 AM   #1
silv3r
LQ Newbie
 
Registered: May 2015
Posts: 11

Rep: Reputation: Disabled
to send a while loop to background


Hi All,

i got a question (noob of course), stuck with my little script when i try to send this while loop into background.

cat stogrpexport | awk 'NR>1' | while read id2 &
do
echo $id2 >> stat.out &
symstat -sid 1075 -i 5 -c 5 -dev $id2 >> stat.out &
done


symstat is a vmax storage command for stat reading which taking long time to complete. is there any better wat to re-write this loop? thanks.
 
Old 05-08-2015, 05:13 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
#instead of
cat filename | awk 'script'
# use just
awk 'script' filename

# do not put & at the end of the lines, especially after while it will do something "strange" for you
# instead, put only the sysstat commands into background.
awk | while
do
echo
simstat .. &
done
# but take care about the result, that may contain mixed result of the simstat commands.
 
Old 05-08-2015, 07:08 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Another, though less likely to bite is the sequence:
Code:
echo $id2 >> stat.out &
symstat -sid 1075 -i 5 -c 5 -dev $id2 >> stat.out &
You can't tell what will happen for sure - the echo may come out after the following command...

I think you would do better with:
Code:
(echo $id2; symstat -sid1075 -i 5 -c 5 -dev $id2) >>stat.out &
But globally, since you are also executing this stuff in background, the output of the multiple instances of symstat could be mixed. In your case, (assuming it takes a bit of time) all the echo output could be together, followed by the symstat commands - in any order....
 
Old 05-09-2015, 11:28 AM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Which part(s) are you trying to send to background? You've got the & after the while, the echo, and the symstat. None of which really make any sense. It might be better to put it all in a script and send the whole script to the background. Or in a function (although I haven't tested if functions could be backgrounded).

You might want the output filename to be driven by the $id2 var, since multi-threading on the same output filename can have many issues. Like the output info being meshed into the file in an unpredictable order. Or the output file being locked and the process having to wait on the previous thread anyway. Plus various risks of so many recursions that you simply don't have enough RAM to keep it all juggling.

You could be wasting effort to, if the symstat program isn't designed to have multiple instances running at the same time. Which you will ultimately have if send one to background and immediately start another. If that program locks file(s), then the 2nd+ instance(s) will have to wait on the previous instance to end anyway. And the 2nd+ instance could prevent the first instance from ending. It's all quite a mess which might be better solved by adding a sleep statement in the loop. sleep 0.3; Not really intuitive, but sometimes the right answer.
 
1 members found this post helpful.
Old 05-09-2015, 12:24 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You may put a loop into background:

Code:
awk | while read; do
    echo
    symstat
done >file &
 
Old 05-11-2015, 10:31 PM   #6
silv3r
LQ Newbie
 
Registered: May 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
shadow_7 - im trying to send the symstat command to background because the symstat itself is a loop
pan64 - yeap, once i remove the & after the while it looks better
jpollard - yeap, single line to send to background is much easier
millgates - works like a charm, perfectly!!!

works perfeclt now, and i cant tail the job to see the progress, its just i dunno why i cant use command "jobs" to check any background jobs - any thought?

However, it works great now, thank you to all that's been helping..cheers guys!!
 
Old 05-12-2015, 02:44 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
would be nice to explain what did you try exactly. I can only guess: you wrote a script and inside the script you started several background processes.
The shell you started (which executed your script) is now completed, therefore the current shell has no more jobs to print. The parent process of your background tasks is now 1 (the init process).
 
Old 05-12-2015, 09:20 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by pan64 View Post
would be nice to explain what did you try exactly. I can only guess: you wrote a script and inside the script you started several background processes.
The shell you started (which executed your script) is now completed, therefore the current shell has no more jobs to print. The parent process of your background tasks is now 1 (the init process).
@OP this is likely important because you could end up complaining that things are out of sync or there's some other form of race condition now in your script which you don't understand.
 
Old 05-13-2015, 09:58 PM   #9
silv3r
LQ Newbie
 
Registered: May 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
there's a typo earlier - "i CAN tail -f the output, its just i cant see the backgound job by typing jobs ".

the symstat command is to check the I/O reading/write activity for i=5 iteration c=5 count for all the devices in stogrpexport. right now it works fine, its just i cant see any background job id for the loop. again, thanks alot for your help
 
  


Reply



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
code that send a process to the background feetyouwell Programming 5 11-29-2010 03:20 PM
Send process to a node in background and logout horacioemilio Programming 4 12-14-2007 12:59 PM
send process to the background ugp Programming 3 02-08-2006 10:28 PM
send commands to background processes chatterbug89 Linux - General 2 07-13-2005 03:55 PM
send process to background radam Linux - General 6 07-13-2004 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:43 PM.

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
Open Source Consulting | Domain Registration