11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

nice<br />

nohup<br />

pidof<br />

Capitolo 13. Coman<strong>di</strong> <strong>di</strong> sistema e d’amministrazione<br />

Esegue un job sullo sfondo (background) con priorità mo<strong>di</strong>ficata. Le priorità vanno da 19 (la più<br />

bassa) a -20 (la più alta). Solo root può impostare le priorità negative (quelle più alte). Coman<strong>di</strong><br />

correlati sono: renice, snice e skill.<br />

Mantiene un comando in esecuzione anche dopo la <strong>di</strong>sconnessione dell’utente. Il comando viene<br />

eseguito come un processo in primo piano (foreground) a meno che non sia seguito da &. Se si usa<br />

nohup in uno script, si prenda in considerazione <strong>di</strong> accoppiarlo a wait per evitare <strong>di</strong> creare un<br />

processo orfano o zombie.<br />

Identifica l’ID <strong>di</strong> processo (PID) <strong>di</strong> un job in esecuzione. Poiché i coman<strong>di</strong> <strong>di</strong> controllo dei job,<br />

come kill e renice, agiscono sul PID <strong>di</strong> un processo (non sul suo nome), è necessario identificare<br />

quel determinato PID. Il comando pidof è approssimativamente simile alla variabile interna $PPID.<br />

bash$ pidof xclock<br />

880<br />

Esempio 13-6. pidof aiuta ad terminare un processo<br />

#!/bin/bash<br />

# kill-process.sh<br />

NESSUNPROCESSO=2<br />

processo=xxxyyyzzz # Si usa un processo inesistente.<br />

# Solo a scopo <strong>di</strong>mostrativo...<br />

# ... con questo script non si vuole terminare nessun processo in esecuzione.<br />

#<br />

# Se però voleste, per esempio, usarlo per scollegarvi da Internet, allora<br />

# processo=pppd<br />

t=‘pidof $processo‘ # Cerca il pid (id <strong>di</strong> processo) <strong>di</strong> $processo.<br />

# Il pid è necessario a ’kill’ (non si può usare ’kill’ con<br />

#+ il nome del programma).<br />

if [ -z "$t" ] # Se il processo non è presente, ’pidof’ restituisce null.<br />

then<br />

echo "Il processo $processo non è in esecuzione."<br />

echo "Non è stato terminato alcun processo."<br />

exit $NESSUNPROCESSO<br />

364

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!