LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-12-2008, 07:17 AM   #1
hamimi
LQ Newbie
 
Registered: Mar 2008
Location: Johannesburg
Posts: 5

Rep: Reputation: 0
Run 2 scripts simultaneously


Hi ,
I have a script:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
sh newfile;monrst

I needs to run "newfile" & "monrst" together so that if newfile is complete it e-mails a message.
NB: my script monrst already has the e-mail script inside to check for process.

THanks
Hamim
 
Old 06-12-2008, 08:30 AM   #2
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
You want to run one or both scripts "in the background."

First, if your scripts start with the line #!/bin/sh or #!/bin/bash and have their executable bit set, then you can run them w/o specifically calling sh or bash. Assuming you have done so, then if you want to (for example) background monrst while newfile newfile runs in the foreground (i.e. normally), your script would like like.

Code:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
monrst &
newfile
See the bash man page for more info.

(For improved readabilty of your posts, see my signature about "code tags.")

EDIT: Alternatively, you can combine the last two lines into the single line

Code:
monrst & newfile

Last edited by blackhole54; 06-12-2008 at 08:33 AM.
 
Old 06-12-2008, 08:11 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
I believe that's double ampersand:

monrst && newfile
 
Old 06-12-2008, 10:32 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by hamimi View Post
Hi ,
I have a script:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
sh newfile;monrst

I needs to run "newfile" & "monrst" together so that if newfile is complete it e-mails a message.
NB: my script monrst already has the e-mail script inside to check for process.

THanks
Hamim
I don't understand the purpose of this all.

That script will read $SOMETHING, change the 7th field on a given file by that $SOMETHING and write it to a new file. The it will call other two different scripts.

If you want to run the "monrst" *after* -and only IF- "newfile" completes, you can just concatenate it with the logical AND operator, which is the double ampersand '&&'. This way:

Code:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
sh newfile && sh monrst
That will ensure that monrst is launched *after* newfile and only if newfile was successful.

Quote:
Originally Posted by blackhole54 View Post
You want to run one or both scripts "in the background."
I don't think he wants to run anything on the background, but since I only understand the OP partially, I can't be sure. In any case, if he wants to ensure synchronization, he must use some bash magic:

Code:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"

# saves the return state
ret=$(sh newfile &)
# captures the PID
pid=$!

# do whatever you want in the middle
# while sh newfile is run in the background

# waits for sh newfile to finish
wait $pid

# and then continue with monrst, if ret is ok
test -z $ret && sh monrst
Completely untested, of course, but I think it should work. $! is bash specific though, so, if you are really using the legacy sh, you are going to need another way to get the pid of the application so you can wait for it to finish.

Quote:
First, if your scripts start with the line #!/bin/sh or #!/bin/bash and have their executable bit set, then you can run them w/o specifically calling sh or bash.
Which is not a good thing to assume if you are going to use this script on many locations, since UIDS are going to be different, and you don't want to go chmoding scripts all the way. Anyway, adding the sh in front of it doesn't hurt in any way and you have one less thing to worry about when scripting.


Quote:
Originally Posted by chrism01 View Post
I believe that's double ampersand:

monrst && newfile
Since I don't fully understand this, I don't really know what do the OP mean. The difference between the two is simple: && is a logical AND operator, and & is a mechanism to put the actual command into the background as long as it's launched (so, other commands can continue running concurrently).

A logical AND is evaluated sequentially, and if any of the members is not true (in bash, that means "non zero"), then the whole sentence is false (and hence, no additional evaluation is needed).

So, if you do "command1 && command2 && command3" and command1 fails, the return state of command1 will be non-zero, and no further commands will be executed (because there's no need to evaluate anything more, because the whole sentence will be false anyway.

On the contrary, the OR operator will continue to run commands until one of them ends ok (zero), which I sometimes use to build quick sentences like this:

Code:
test -f /foo && echo "/foo exists" || echo "/foo doesn't exist"
Which would be some kind of quick if...else construct.
 
Old 06-13-2008, 12:33 AM   #5
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by chrism01 View Post
I believe that's double ampersand:

monrst && newfile
No. A double ampersand will run them both in the foreground, sequentially, with the second script being run only if the first script returns an exit code of 0. A single ampersand will start the first script in the background and then immediately and unconditionally start the second in the foreground. In this case the ampersand has the normal function of backgrounding, but also servers the addtional role of separating, like a semicolon would if you weren't backgrounding. As I said, that single line is completely equivalent to the last two lines in the previous code box.


EDIT:

Quote:
Originally Posted by i92guboj View Post
Which is not a good thing to assume if you are going to use this script on many locations, since UIDS are going to be different, and you don't want to go chmoding scripts all the way. Anyway, adding the sh in front of it doesn't hurt in any way and you have one less thing to worry about when scripting.
@hamimi,

If you wish (as suggested by i92guboj) to continue to call sh explicitly, then to background newfile and then start monrst in the foreground:

Code:
sh newfile & sh monrst
Please let us know if any of us have interpreted you question correctly.

Last edited by blackhole54; 06-13-2008 at 01:01 AM.
 
Old 06-17-2008, 04:15 AM   #6
hamimi
LQ Newbie
 
Registered: Mar 2008
Location: Johannesburg
Posts: 5

Original Poster
Rep: Reputation: 0
Hi
Yes -All you guys have made a good explanation of how to run in Background & also test if first script has completed to run second script

thanks ALot for all your help
Hamim
 
  


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
How to run Windows and Linux simultaneously Bjwebb Linux - Newbie 21 05-13-2011 02:18 AM
Can php4 and php5 run simultaneously on apache2? tisource Linux - Software 1 09-15-2005 12:53 AM
Run knoppix and xp simultaneously Vashtheddrfreak Linux - Newbie 6 05-24-2004 11:31 AM
can we run graphics and text mode simultaneously kusum Linux - Software 1 02-09-2004 05:40 AM
how to run Squid and Apache on port 80 simultaneously. gagan_fzks Linux - Networking 1 11-12-2003 06:15 AM


All times are GMT -5. The time now is 04:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration