views:

34

answers:

3

Hello,

I want to be able to echo some stuff (i.e. a spinner) as long as a command (a database dump to be precise), is still running.

Because right now, it's like 15 minutes of: "is my script still running?"

Suggestions in bash? while? until?

Thanks,

A: 

while - and check that PID of the running process is still running. You can get the PID using $?

Drakosha
Assuming the process is started in the background, the PID is in `$!`
Peter van der Heijden
A: 

In a script:

#!/bin/bash
your-prog &
while jobs %?your-prog > /dev/null 2>&1; do echo -ne '/\r'; sleep 1; echo -ne '\\\r'; sleep 1; done
Dennis Williamson
+1  A: 

Here's a little script that displays a spinner until the command given to it is done.

#!/bin/bash

if [[ -z "$*" ]] ; then
    echo "Usage:  $0 command [args] ..."
    exit 0
fi

"$@" &
echo -n "Running '$@'... "

BACKSPACE='\010'
SPINNER='|/-\'
INDEX=0
while kill -0 $! 2>/dev/null ; do
    echo -n -e "${SPINNER:$INDEX:1}"
    sleep 0.2
    (( INDEX = ($INDEX + 1) % 4 ))
    echo -n -e "$BACKSPACE"
done
echo -e "${BACKSPACE}done."

Example usage:

$ ./spinner sleep 3
Running 'sleep 3'... [spinner displayed here]

After a while:

$ ./spinner sleep 3
Running 'sleep 3'... done.
$

(My way of bash scripting is based on trial-and-error, so suggestions for improving this script are very welcome.)

Thomas