0

I want to write a shell script which

  1. initiate a process which that gets data from a data base and and writes to a file (time to do this varies from 5 min - few hours)
  2. Use the data in the generated file and do some calculations

What command can I use in shell to pause till my first process is complete before executing the second part as the second part should not start till writing to the file is complete?

8
  • Does the command for the first part return before it is finished (i.e. does it run in the background)? Because if it only returns when it is finished then you don't need to do anything for this. If it does run in the background then do you have some way of being able to know when it is finished? Commented Apr 2, 2015 at 16:44
  • Yes it returns before it is finished and tries to execute the second part while the writing to a file is not yet complete. Commented Apr 2, 2015 at 16:48
  • Does it signal completion somehow? How do you know when it is finished? Commented Apr 2, 2015 at 16:51
  • Currently I have no good method unless looking at the processes which are running. or do a tail -f on the file which is being written Commented Apr 2, 2015 at 16:54
  • 1
    You can sleep-loop and poll for a specifically named process (with pgrep), yes. Commented Apr 2, 2015 at 17:06

2 Answers 2

1

Well, you don't really need a command. Your shell should simply block until a spawned child terminates:

proc1
proc2 # <- will not be called until proc1 is done.

Things only get complicated when you do stuff like this:

proc1 &
proc2 # <- will start immediately after proc1 was launched

In that case you would have to wait, as Politank-Z points out:

proc1 &
wait
proc2

Which doesn't really make any sense.


Edit: After your clarification

You have to get the first process to communicate to you the process id of its child and check for that to finish. A crude attempt would be periodically checking for

ps x | grep -v grep | grep "$spawnedPID"

(Assuming the child processes is not the grep command itself)

Sign up to request clarification or add additional context in comments.

2 Comments

You can't wait on non-child processes.
@EtanReisner: You are technically correct. The best kind of correct. I thought you were able to wait on grand-child processes. Editing ...
0

I think you are looking for the wait command, but it depends on how part 1 is started.

2 Comments

This won't help if the process in part 1 is self-backgrounding or a daemon or some-such. It will if he manually backgrounds it but at that point it would be simpler to simply not do that (unless that's necessary for some unspecified part of this task).
@EtanReisner: OP asks about till my first process is complete. If you self-background, you are technically done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.