This script always checks roscore liveness and automatically run and kill a program.

rosrun jsk_tools rostopic echo /foo


$ rosrun jsk_tools -h
usage: [-h] [--respawn] [--timeout TIMEOUT]
                             [--ping-trials PING_TRIALS]
                             [--sigint-timeout SIGINT_TIMEOUT]
                             [--sigterm-timeout SIGTERM_TIMEOUT]

positional arguments:

optional arguments:
  -h, --help            show this help message and exit
  --respawn, -r         respawn if child process stops
  --timeout TIMEOUT     Timeout to verify if rosmaster is alive by ping
                        command in seconds
  --ping-trials PING_TRIALS
                        If ping fails PING-TRIALS times, master is regarded as
  --sigint-timeout SIGINT_TIMEOUT
                        Timeout to escalete from sigint to sigterm to kill
                        child processes
  --sigterm-timeout SIGTERM_TIMEOUT
                        Timeout to escalete from sigterm to sigkill to kill
                        child processes