2012-06-15 6 views
5

Sto scrivendo uno script e ad un certo punto chiamo "command1" che non si arresta finché non viene richiamato CTRL + C.Comandi Linux - interruzione del funzionamento con timeout

  1. C'è un modo per stabilire un timeout per un comando? Come: argomenti comando1 -timeout 10
  2. Come si scrive un comando CTRL + C in forma testuale?

Thx!

+0

Che cosa si intende esattamente inviando un Ctrl + C in forma testuale? – betabandido

risposta

6

È possibile utilizzare il comando timeout da coreutils GNU (potrebbe essere necessario installare prima, ma si tratta nella maggior parte, se non tutte, le distribuzioni Linux):

timeout [OPTION] DURATION COMMAND [ARG]... 

Per esempio:

timeout 5 ./test.sh 

interromperà lo script dopo 5 secondi di esecuzione. Se si desidera inviare un segnale KILL (anziché TERM), utilizzare il flag -k.

Here hai la descrizione completa del comando timeout.

0

Ho appena provato

jekyll -server & sleep 10;pkill jekyll 

Potrebbe fare per la situazione.

+1

Attenzione: ucciderai tutti i processi chiamati "jekyll" con questo. È molto meglio salvare il PID di quello che ti interessa (è in '$!' Dopo il lancio del comando) e uccidere solo quello. – Celada

-1

nello script è possibile impostare un'attesa. wait 10 attendere 10 secondi e per uscire dal programma senza CTRL + C esaminare il comando exit. Se usi exit 0 significa ok. Ci sono diverse versioni, ma non so cosa significano esattamente in cima alla mia testa.

exit 1 
exit 2..... so on and so forth 

Aggiornamento


@ Celada

Non c'è bisogno di colpire. Potresti aver appena detto "forse non hai capito bene la domanda" Stackoverflow è qui per aiutare le persone a imparare, non per abbatterle. Hanno inventato Reddit per questo. Per quanto riguarda i codici di uscita, è possibile forzare l'uscita del programma inviando il comando exit() con un codice. Direttamente dal linux.die.net.

Exit Code Number Meaning Example Comments 
1 Catchall for general errors let "var1 = 1/0" Miscellaneous errors, such as "divide by zero" 
2 Misuse of shell builtins (according to Bash documentation) Seldom seen, usually defaults to exit code 1 
126 Command invoked cannot execute Permission problem or command is not an executable 
127 "command not found" Possible problem with $PATH or a typo 
128 Invalid argument to exit exit 3.14159 exit takes only integer args in the range 0 - 255 (see footnote) 
128+n Fatal error signal "n" kill -9 $PPID of script $? returns 137 (128 + 9) 
130 Script terminated by Control-C Control-C is fatal error signal 2, (130 = 128 + 2, see above) 
255* Exit status out of range exit -1 exit takes only integer args in the range 0 - 255 
+0

Questo non ha senso. Questo non è affatto ciò che fa il comando 'wait', e non ho idea di come si debba usare' exit' per essere usato. – Celada