Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using wait from sorted by
Terminal - Commands using wait - 7 results
wait 536; anywait 536; anywaitd 537; anywaitp 5562 5563 5564
2014-10-22 06:31:47
User: colemar
Functions: wait
0

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.

sleep 10 & wait $!
2014-09-25 13:33:51
User: yorkou
Functions: sleep wait
1

A nice way to interrupt a sleep with a signal.

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
2012-03-31 10:03:58
User: julnegre
Functions: sh wait
0

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

(sp-sc sop://broker.sopcast.com:3912/80562 8908 10999 &>/dev/null &); sleep 10; wait $(vlc http://localhost:10999); killall sp-sc
2011-04-06 00:08:38
User: Bonster
Functions: killall sleep wait
0

requires sp-auth installed

This command will auto kill sp-sc after vlc is closed, so u wont have to do it manually

wait $!
2010-06-07 21:56:36
User: noahspurrier
Functions: wait
2

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.

$COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
2010-05-26 11:12:26
User: keymon
Functions: kill sleep wait
3

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

}

wait
2010-01-15 04:03:11
User: bhepple
Functions: wait
0

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