Commands using wait (7)

  • 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

    wait 536; anywait 536; anywaitd 537; anywaitp 5562 5563 5564
    colemar · 2014-10-22 06:31:47 0
  • A nice way to interrupt a sleep with a signal. Show Sample Output

    sleep 10 & wait $!
    yorkou · 2014-09-25 13:33:51 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 script more infos :

    sh 1 20 & var1="$!" & sh 2 10 & var2="$!" & sh 3 40 & var3="$!" & sh 4 30 & var4="$!" ; wait $var1 && wait $var2 && wait $var3 && wait $var4
    julnegre · 2012-03-31 10:03:58 0
  • requires sp-auth installed This command will auto kill sp-sc after vlc is closed, so u wont have to do it manually

    (sp-sc sop:// 8908 10999 &>/dev/null &); sleep 10; wait $(vlc http://localhost:10999); killall sp-sc
    Bonster · 2011-04-06 00:08:38 0
  • 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.

    wait $!
    noahspurrier · 2010-06-07 21:56:36 0
  • 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 }

    $COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
    keymon · 2010-05-26 11:12:26 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

    bhepple · 2010-01-15 04:03:11 2

What's this? 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

the same as [Esc] in vim
Faster and more convinent than [Esc]

for newbies, how to get one line info about all /bin programs
Get simple description on each file from /bin dir, in list form, usefull for newbies.

Broadcast your shell thru ports 5000, 5001, 5002 ...
run 'nc yourip 5000', 'nc yourip 5001' or 'nc yourip 5002' elsewhere will produce an exact same mirror of your shell. This is handy when you want to show someone else some amazing stuff in your shell without giving them control over it.

Inverted cowsay
It's quite fun to invert text using "" (ref: ). Slightly more challenging is to flip a whole "cowsay". :-)

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

sudo for launching gui apps in background
Need package: gksu Note: Launching gui app in background that needs sudo, won't work great with our old friendly style of launching: $ sudo gedit /etc/passwd & because this would put sudo in background ! Using gksudo as demonstrated, would popup a gui sudo window. May be this is a common knowledge, but not knowing this frustrated me during my newbie year.

command line Google I'm Feeling Lucky
opens the Google I'm Feeling Lucky result in lynx, the command line browser

Who needs pipes?
or: C

Convert JSON to YAML
Convert JSON to YAML. Note that you'll need to have PyYaml installed.

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.


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: