Wait Until a Process Ends

Wait until a process ends

I think you just want this:

var process = Process.Start(...);
process.WaitForExit();

See the MSDN page for the method. It also has an overload where you can specify the timeout, so you're not potentially waiting forever.

Wait for process to finish, or user input

Use kill -0 to validate that the process is still there and read with a timeout of 0 to test for user input. Something like this?

pid=$!
while kill -0 $pid; do
read -t 0 && exit
sleep 1
done

wait one process to finish and execute another process

You can achieve a simple way of process synchronization in bash using wait which waits for one or more number of background jobs to complete before running the next.

You generally run jobs in the background by appending the & operator to the end of a command. At that point the PID (process ID) of the newly created background process is stored in a special bash variable: $! and wait command allows this process to be terminate before running the next instruction.

This can be demonstrated by a simple example

$ cat mywaitscript.sh

#!/bin/bash

sleep 3 &

wait $! # Can also be stored in a variable as pid=$!

# Waits until the process 'sleep 3' is completed. Here the wait on a single process is done by capturing its process id

echo "I am waking up"

sleep 4 &
sleep 5 &

wait # Without specifying the id, just 'wait' waits until all jobs started on the background is complete.

echo "I woke up again"

Command ouput

$ time ./mywaitscript.sh
I am waking up
I woke up again

real 0m8.012s
user 0m0.004s
sys 0m0.006s

You can see the script has taken ~8s to run to completion. The breakdown on the time is

  1. sleep 3 will take full 3s to complete its execution

  2. sleep 4 and sleep 5 are both started sequentially one after next and it has taken the max(4,5) which is approximately ~5s to run.

You can apply the similar logic to your question above. Hope this answers your question.

Node.JS: how to wait for a process to finish before continuing?

downloadAndSaveFiles returns a promise (because the function is async) but that promise doesn't "wait" for https.get or fs.createWriteStream to finish, and therefore none of the code that calls downloadAndSaveFiles can properly "wait".

If you interact with callback APIs you cannot really use async/await. You have to create the promise manually. For example:

const downloadAndSaveFiles = ({ url, dir }) => {
return new Promise((resolve, reject) => {
// TODO: Error handling
https.get(url, (res) => {
// File will be stored at this path
console.log('dir: ', dir);
var filePath = fs.createWriteStream(dir);
filePath.on('finish', () => {
filePath.close();
console.log('Download Completed');
resolve(); // resolve promise once everything is done
});
res.pipe(filePath);
});
});
};

Wait for process to finish before proceeding in Java

Call Process#waitFor(). Its Javadoc says:

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

Bonus: you get the exit value of the subprocess. So you can check whether it exited successfully with code 0, or whether an error occured (non-zero exit code).



Related Topics



Leave a reply



Submit