Wait for process to finish, or user input

Michael Plotke

I have a backgrounded process that I would like to wait for (in case it fails or dies), unless I receive user input. Said another way, the user input should interrupt my waiting.

Here's a simplified snippet of my code

#!/bin/bash
...

mplayer -noconsolecontrols "$media_url" &
sleep 10 # enough time for it to fail

ps -p $!
if [ $? -ne 0 ]
then
    fallback
else
    read
    kill $!
fi

The line that I particularly dislike is sleep 10, which is bad because it could be too much time, or not enough time.

Is there a way to wait $! || read or the equivalent?

Mike Andrews

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wait for a process to finish OR for the user to press a key

From Dev

AutoHotKey: wait for process, breaking on user input

From Dev

wait a process to finish in Python script

From Dev

Wait for user to finish interacting with UITableView

From Dev

Wait for user to finish interacting with UITableView

From Dev

wait for the process to finish to start another process

From Dev

wait one process to finish and execute another process

From Dev

wait for the process to finish to start another process

From Dev

Run methods in new process and wait for them to finish

From Dev

Ruby: wait for any child process to finish

From Dev

How to wait for a background process to finish in android

From Dev

Wait for async process to finish before redirect in koajs

From Dev

Ruby: wait for any child process to finish

From Dev

How to wait for the copying process (cp) to finish?

From Dev

Bash to Call PHP, Wait for PHP Process to finish?

From Dev

Run methods in new process and wait for them to finish

From Dev

TThread Wait for User Input

From Dev

Wait for user input

From Dev

Send process' STDOUT to screen and second process, but wait for both to finish

From Dev

How to wait for a child process to finish in Node.js?

From Dev

Python Gtk2 & Vte wait for the process Pid is finish

From Dev

Batch - How to wait for process with specific name so start and finish?

From Dev

Need Access VBA to wait for Excel process to finish before resuming

From Dev

Wait for background processes to finish with inverse match process ID

From Dev

Python Gtk2 & Vte wait for the process Pid is finish

From Dev

Batch - How to wait for process with specific name so start and finish?

From Dev

For loop and mysql node how to wait for all queries to finish to process view

From Dev

Why when the first time process go into the loop, it doesn't stop and wait for user input string first?

From Java

C++ wait for user input

Related Related

  1. 1

    Wait for a process to finish OR for the user to press a key

  2. 2

    AutoHotKey: wait for process, breaking on user input

  3. 3

    wait a process to finish in Python script

  4. 4

    Wait for user to finish interacting with UITableView

  5. 5

    Wait for user to finish interacting with UITableView

  6. 6

    wait for the process to finish to start another process

  7. 7

    wait one process to finish and execute another process

  8. 8

    wait for the process to finish to start another process

  9. 9

    Run methods in new process and wait for them to finish

  10. 10

    Ruby: wait for any child process to finish

  11. 11

    How to wait for a background process to finish in android

  12. 12

    Wait for async process to finish before redirect in koajs

  13. 13

    Ruby: wait for any child process to finish

  14. 14

    How to wait for the copying process (cp) to finish?

  15. 15

    Bash to Call PHP, Wait for PHP Process to finish?

  16. 16

    Run methods in new process and wait for them to finish

  17. 17

    TThread Wait for User Input

  18. 18

    Wait for user input

  19. 19

    Send process' STDOUT to screen and second process, but wait for both to finish

  20. 20

    How to wait for a child process to finish in Node.js?

  21. 21

    Python Gtk2 & Vte wait for the process Pid is finish

  22. 22

    Batch - How to wait for process with specific name so start and finish?

  23. 23

    Need Access VBA to wait for Excel process to finish before resuming

  24. 24

    Wait for background processes to finish with inverse match process ID

  25. 25

    Python Gtk2 & Vte wait for the process Pid is finish

  26. 26

    Batch - How to wait for process with specific name so start and finish?

  27. 27

    For loop and mysql node how to wait for all queries to finish to process view

  28. 28

    Why when the first time process go into the loop, it doesn't stop and wait for user input string first?

  29. 29

    C++ wait for user input

HotTag

Archive