Commands using wait (7)

  • I like much more the perl solution, but without using perl. It launches a backgroup process that will kill the command if it lasts too much. A bigger function: check_with_timeout() { [ "$DEBUG" ] && set -x COMMAND=$1 TIMEOUT=$2 RET=0 # Launch command in backgroup [ ! "$DEBUG" ] && exec 6>&2 # Link file descriptor #6 with stderr. [ ! "$DEBUG" ] && exec 2> /dev/null # Send stderr to null (avoid the Terminated messages) $COMMAND 2>&1 >/dev/null & COMMAND_PID=$! [ "$DEBUG" ] && echo "Background command pid $COMMAND_PID, parent pid $$" # Timer that will kill the command if timesout sleep $TIMEOUT && ps -p $COMMAND_PID -o pid,ppid |grep $$ | awk '{print $1}' | xargs kill & KILLER_PID=$! [ "$DEBUG" ] && echo "Killer command pid $KILLER_PID, parent pid $$" wait $COMMAND_PID RET=$? # Kill the killer timer [ "$DEBUG" ] && ps -e -o pid,ppid |grep $KILLER_PID | awk '{print $1}' | xargs echo "Killing processes: " ps -e -o pid,ppid |grep -v PID | grep $KILLER_PID | awk '{print $1}' | xargs kill wait sleep 1 [ ! "$DEBUG" ] && exec 2>&6 6>&- # Restore stderr and close file descriptor #6. return $RET }


    4
    $COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
    keymon · 2010-05-26 11:12:26 3
  • Referring to the original post, if you are using $! then that means the process is a child of the current shell, so you can just use `wait $!`. If you are trying to wait for a process created outside of the current shell, then the loop on `kill -0 $PID` is good; although, you can't get the exit status of the process.


    3
    wait $!
    noahspurrier · 2010-06-07 21:56:36 4
  • A nice way to interrupt a sleep with a signal. Show Sample Output


    1
    sleep 10 & wait $!
    yorkou · 2014-09-25 13:33:51 8
  • If you really _must_ use a loop, this is better than parsing the output of 'ps': PID=$! ;while kill -0 $PID &>/dev/null; do sleep 1; done kill -0 $PID returns 0 if the process still exists; otherwise 1


    0
    wait
    bhepple · 2010-01-15 04:03:11 5
  • requires sp-auth installed This command will auto kill sp-sc after vlc is closed, so u wont have to do it manually


    0
    (sp-sc sop://broker.sopcast.com:3912/80562 8908 10999 &>/dev/null &); sleep 10; wait $(vlc http://localhost:10999); killall sp-sc
    Bonster · 2011-04-06 00:08:38 3
  • This command explains how to manage some asynchronous PID in a global process. The command uses 4 processes in a global process. The asynchronous scripts are simulated by a time.sh script more infos : http://code-esperluette.blogspot.fr/2012/03/bash-gestion-de-processus-asynchrones.html http://www.youtube.com/watch?v=TxsPyAtD70I


    0
    sh time.sh 1 20 & var1="$!" & sh time.sh 2 10 & var2="$!" & sh time.sh 3 40 & var3="$!" & sh time.sh 4 30 & var4="$!" ; wait $var1 && wait $var2 && wait $var3 && wait $var4
    julnegre · 2012-03-31 10:03:58 14
  • Silent: anywait () { for pid in "$@"; do while kill -0 "$pid" >/dev/null 2>&1; do sleep 0.5; done; done } Prints dots: anywaitd () { for pid in "$@"; do while kill -0 "$pid" >/dev/null 2>&1; do sleep 0.5; echo -n '.'; done; done } Prints process ids: anywaitp () { for pid in "$@"; do while kill -0 "$pid" >/dev/null 2>&1; do sleep 0.5; echo -n $pid' '; done; echo; done } You cannot anywait for other users processes. Show Sample Output


    0
    wait 536; anywait 536; anywaitd 537; anywaitp 5562 5563 5564
    colemar · 2014-10-22 06:31:47 8

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

tail -f a log file over ssh into growl

Display disk partition sizes
It is the same but more faster real 0m0,007s user 0m0,011s sys 0m0,000s with my solution real 0m0,038s user 0m0,044s sys 0m0,000s with your solution :)

dump database from postgresql to a file

Keep a copy of the raw Youtube FLV,MP4,etc stored in /tmp/
Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Change the homepage of Firefox
Pros: Works in all Windows computers, most updated and compatible command. Cons: 3 liner Replace fcisolutions.com with your site name.

Copy without overwriting

Fast, built-in pipe-based data sink
This is shorter and actually much faster than >/dev/null (see sample output for timings) Plus, it looks like a disappointed face emoticon.

bash screensaver off

return the latest kernel version from a Satellite / Spacewalk server software channel


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: