running two linux commands sequentially in background

hi
i have cmd1 , cmd2
if i write
cmd1 &
cmd2 &
in a script cmd1 abd cmd2 both starts getting executed parallely in background.
i want first cmd1 to run in background and once it finishes execution cmd2 should be executed.
how to do that ?
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
Just write:

cmd1
cmd2

If you want to run the 2 commands in the background and wait for them, it is the same as just running them sequentially.

Are the 2 cmd's just single commands or scripts maybe?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
i think that will work but will it be a background process ? as we havent specified &
Gerwin Jansen, EE MVETopic Advisor Commented:
Is the background process a script?
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Rohit BajajAuthor Commented:
no i mean i want cmd1 to execute as a background process. like the normal way we specify cmd1 & and linux runs it as a background process
Gerwin Jansen, EE MVETopic Advisor Commented:
You can use the wait command for that, 'wait' on the process id of the background process to finish:

cmd1 &
wait
cmd2 &
wait
gheistCommented:
(cmd1 ; cmd2) &
Gerwin Jansen, EE MVETopic Advisor Commented:
Difference between the above suggestion and the previous one is that the script that uses wait will wait after cmd2 has finished to continue. When starting the above command, the script immediately continues after sending cmd1 ; cmd2 to the background.
gheistCommented:
Do you feel guilty that your script is two lines too wasteful?
Gerwin Jansen, EE MVETopic Advisor Commented:
@gheist - Not at all, the asker doesn't seem very experienced with Linux so instead of just posting one liners, I'm suggesting basic solutions whilst adding an explanation.
jmcgOwnerCommented:
Robinsuri, is this just a question about mechanics or is there some purpose behind running commands in background?

I favor gheist's answer: the two commands are run asynchronously in a background process. Whether or not you need to issue a wait for them depends on why you're running them in the background and what other options you may have for achieving rendezvous of the parallel streams.
Rohit BajajAuthor Commented:
there is purpose.
cmd1 for me is stopping the server
cmd2 start the server
probably i dont need background thing...but yes i do need to execute cmd2 after cmd1 finishes
Gerwin Jansen, EE MVETopic Advisor Commented:
Then you won't need background at all, my first suggestion should work just fine. What kind of server/process are you stopping/starting btw? The cmd's may give feedback that you could be interested in, for showing something on screen maybe.

But if the cmd's are just sending a stop/start command to a server process then we'd have to look into log files to determine whether the server has successfully been stopped or started.
gheistCommented:
(cmd1 ; cmd2) &
will do exactly that in background
cmd1 ; cmd2
will do that in foreground
cmd1 || cmd-err && cmd2
will execute cmd-err (like logger or killall) when 1st command returns non-zero exit code or cmd2 otherwise
Rohit BajajAuthor Commented:
i am stoping the webserver jetty and then starting it again.
Gerwin Jansen, EE MVETopic Advisor Commented:
Can you have a look at that web server's log file? Does it log when the startup has completed successfully and when it has shutdown? That is something you could 'wait' for before continuing your script.
gheistCommented:
Most likely you need to wrap jetty with tanuki wrapper or jsvc (whichever is present on your target OS)
Or just add few seconds sleep to let old jetty to terminate.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.