LSB sta per Linux Standard Base.
LSB è stato avviato da Linux Foundation per ridurre la differenza tra diverse distribuzioni Linux e quindi ridurre i costi legati al porting tra diverse distribuzioni. Gli script di inizializzazione sono uno di questi da standardizzare.
In questo articolo vedremo come scrivere uno script Init conforme a LSBInit Standard.
Gli script Init vengono utilizzati per avviare|interrompere un software|servizio. Ad esempio, se stai usando il software postgresql, avremo uno script Init chiamato '/etc/init.d/postgresql' che può essere usato per 'start|stop|restart|reload|force-reload|status'.
Gli script di inizializzazione conformi a LSB devono:
- Fornire almeno "avvio, arresto, riavvio, ricaricamento forzato e stato"
- Restituisci il codice di uscita corretto
- Dipendenze di runtime del documento
Facoltativamente, possono utilizzare funzioni init.d come "log_success_msg", "log_failure_msg" ecc. per registrare i messaggi.
LSB fornisce un insieme predefinito di funzioni che si trova in /lib/lsb/init-functions. Possiamo utilizzare queste funzioni nei nostri script Init. Per impostazione predefinita, tutto il processo Init registrerà il pid del processo in un file nella directory /var/run/. Questo è utile per gli script Init per trovare lo stato del processo.
Ora iniziamo a scrivere uno script Init. Per prima cosa dobbiamo aggiungere un'intestazione allo script Init, che assomiglia a,
### BEGIN INIT INFO # Provides: my_daemon # Required-Start: postgresql networking # Required-Stop: postgresql networking # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: This is a test daemon # Description: This is a test daemon # This provides example about how to # write a Init script. ### END INIT INFO
Fornisce specifica qual è la funzione fornita da questo script Init. Il nome dovrebbe essere univoco.
Inizio obbligatorio specifica l'insieme di strutture che devono essere avviate prima di avviare questo servizio. Nel nostro caso, postgresql e networking devono essere avviati prima di avviare my_daemon. Ricorda, qui "postgresql" avrà uno script Init separato che dice "Fornisce:postgresql". Ciò garantisce l'avvio basato sulle dipendenze. Sulla base di questa dipendenza, il comando "inserv" o "update-rc.d" inserirà le voci nelle directory di livello di esecuzione con il numero di sequenza appropriato. Ad esempio, le voci possono essere come segue
/etc/rc2.d/S12postgresql /etc/rc2.d/S03networking /etc/rc2.d/S13my_daemon
Ciò indica che la rete verrà avviata prima, quindi postgresql e quindi my_daemon.
Per saperne di più sui livelli di esecuzione, fare riferimento alla fase n. 6 (che è il livello di esecuzione) del processo di avvio di Linux.
Sosta obbligatoria specifica l'elenco delle strutture che devono essere arrestate solo dopo l'arresto di questa struttura. Qui solo dopo l'arresto di my_daemon, le funzionalità di postgresql e di rete verranno interrotte.
Inizio predefinito Interruzione predefinita definisce i livelli di esecuzione in cui il servizio deve essere avviato o interrotto.
Breve descrizione e Descrizione sono utilizzati per fornire una descrizione in merito alla struttura fornita. La descrizione può estendersi su più righe, la descrizione breve è limitata a una singola riga.
Diamo un'occhiata a un vero e proprio script Init.
### BEGIN INIT INFO
# Provides: my_daemon
# Required-Start: postgresql networking
# Required-Stop: postgresql networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This is a test daemon
# Description: This is a test daemon
# This provides example about how to
# write a Init script.
### END INIT INFO
# Using the lsb functions to perform the operations.
. /lib/lsb/init-functions
# Process name ( For display )
NAME=my-daemon
# Daemon name, where is the actual executable
DAEMON=/home/user1/my_daemon
# pid file for the daemon
PIDFILE=/var/run/my_daemon.pid
# If the daemon is not there, then exit.
test -x $DAEMON || exit 5
case $1 in
start)
# Checked the PID file exists and check the actual status of process
if [ -e $PIDFILE ]; then
status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
# If the status is SUCCESS then don't need to start again.
if [ $status = "0" ]; then
exit # Exit
fi
fi
# Start the daemon.
log_daemon_msg "Starting the process" "$NAME"
# Start the daemon with the help of start-stop-daemon
# Log the message appropriately
if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON ; then
log_end_msg 0
else
log_end_msg 1
fi
;;
stop)
# Stop the daemon.
if [ -e $PIDFILE ]; then
status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?"
if [ "$status" = 0 ]; then
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
/bin/rm -rf $PIDFILE
fi
else
log_daemon_msg "$NAME process is not running"
log_end_msg 0
fi
;;
restart)
# Restart the daemon.
$0 stop && sleep 2 && $0 start
;;
status)
# Check the status of the process.
if [ -e $PIDFILE ]; then
status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
else
log_daemon_msg "$NAME Process is not running"
log_end_msg 0
fi
;;
reload)
# Reload the process. Basically sending some signal to a daemon to reload
# it configurations.
if [ -e $PIDFILE ]; then
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PIDFILE --name $NAME
log_success_msg "$NAME process reloaded successfully"
else
log_failure_msg "$PIDFILE does not exists"
fi
;;
*)
# For invalid arguments, print the usage message.
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 2
;;
esac Lo script sopra fornisce fondamentalmente un modello per la scrittura di script LSBInit. Puoi modificare le variabili DAEMON,PIDFILE,NAME e l'intestazione per adattare questo script ai tuoi programmi.
Per saperne di più sulle funzioni fornite da LSB, fare riferimento a Funzioni InitScript
Una volta terminato lo script Init, per avviare o arrestare automaticamente lo script, eseguire il comando seguente. Ciò aggiungerà voci "S" e "K" appropriate nei livelli di esecuzione indicati. Ciò aggiungerà anche il numero di sequenza appropriato considerando le dipendenze.
update-rc.d filename defaults