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.

#+ stringa <strong>di</strong> lunghezza zero,<br />

then # significa che nessun processo in esecuzione<br />

#+ corrisponde al pid dato.<br />

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

exit $E_ERR_PROCESSO<br />

fi<br />

Capitolo 27. /dev e /proc<br />

# In alternativa:<br />

# if ! ps $1 > /dev/null 2>&1<br />

# then # nessun processo in esecuzione corrisponde al pid dato.<br />

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

# exit $E_ERR_PROCESSO<br />

# fi<br />

# Per semplificare l’intera procedura, si usa "pidof".<br />

if [ ! -r "/proc/$1/$FILEPROC" ] # Controlla i permessi in lettura.<br />

then<br />

echo "Il processo $1 è in esecuzione, ma..."<br />

echo "Non ho il permesso <strong>di</strong> lettura su /proc/$1/$FILEPROC."<br />

exit $E_ERR_PERMESSO # Un utente or<strong>di</strong>nario non può accedere ad alcuni<br />

#+ file <strong>di</strong> /proc.<br />

fi<br />

# Le due ultime verifiche possono essere sostituite da:<br />

# if ! kill -0 $1 > /dev/null 2>&1 # ’0’ non è un segnale, ma<br />

# verifica la possibilità<br />

# <strong>di</strong> inviare un segnale al processo.<br />

# then echo "Il PID non esiste o non sei il suo proprietario" >&2<br />

# exit $E_ERR_PID<br />

# fi<br />

file_exe=$( ls -l /proc/$1 | grep "exe" | awk ’{ print $11 }’ )<br />

# Oppure file_exe=$( ls -l /proc/$1/exe | awk ’{print $11}’ )<br />

#<br />

# /proc/numero-pid/exe è un link simbolico<br />

#+ al nome completo del processo chiamante.<br />

if [ -e "$file_exe" ] # Se /proc/numero-pid/exe esiste...<br />

then # esiste anche il corrispondente processo.<br />

echo "Il processo nr.$1 è stato invocato da $file_exe."<br />

else<br />

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

fi<br />

# Questo elaborato script si potrebbe *quasi* sostituire con<br />

# ps ax | grep $1 | awk ’{ print $5 }’<br />

# Questa forma, però, non funzionerebbe...<br />

# perché il quinto campo <strong>di</strong> ’ps’ è l’argv[0] del processo,<br />

# non il percorso del file eseguibile.<br />

508

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

Saved successfully!

Ooh no, something went wrong!