Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

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 ?
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

i think that will work but will it be a background process ? as we havent specified &
Is the background process a script?
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
You can use the wait command for that, 'wait' on the process id of the background process to finish:

cmd1 &
wait
cmd2 &
wait
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Do you feel guilty that your script is two lines too wasteful?
@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.
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.
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
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.
(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
i am stoping the webserver jetty and then starting it again.
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.
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.