1
4

1 回答 1

2

As long as the job is running in the foreground, keys will not be passed to the shell. So setting a key binding for killing a foreground process and starting it again won't work.

But as you could start your server in an endless loop, so that it restarts automatically. Assuming the name of the command is run_server you can start it like this on the shell:

(TRAPINT(){};while sleep .5; do run_server; done)

The surrounding parentheses start a sub-shell, TRAPINT(){} disables SIGINT for this shell. The while loop will keep restarting run_server until sleep exits with an exit status that is not zero. That can be achieved by interrupting sleep with ^C. (Without setting TRAPINT, interrupting run_server could also interrupt the loop)

So if you want to restart your server, just press ^C and wait for 0.5 seconds. If you want to stop your server without restarting, press ^C twice in 0.5 seconds.

To save some typing you can create a function for that:

doloop() {(
    TRAPINT(){}
    while sleep .5
    do
        echo running \"$@\"
        eval $@
    done   
)}

Then call it with doloop run_server. Note: You still need the additional surrounding () as functions do not open a sub-shell by themselves.

eval allows for shell constructs to be used. For example doloop LANG=C locale. In some cases you may need to use (single):

$ doloop echo $RANDOM
running "echo 242"
242
running "echo 242"
242
running "echo 242"
242
^C
$ doloop 'echo $RANDOM'
running "echo $RANDOM"
10988
running "echo $RANDOM"
27551
running "echo $RANDOM"
8910
^C
于 2014-01-24T13:16:26.757 回答