Per un cliente avevo bisogno di un modo rapido per inviare un’e-mail ogni volta che il suo sistema (centos) si arrestava, faceva un reboot, o semplicemente boot.
Ho trovato un utile articolo su http://www.syntaxtechnology.com che ne parlava e la maggior parte di questo articolo si basa su questo.
Per ottenere una e-mail sia allo start up che all’arresto abbiamo bisogno di scrivere uno script di init. I suggerimenti riportati di seguito sono specifici per un sistema basato su Red Hat (Red Hat, Fedora, CentOS, ecc), ma dovrebbe essere abbastanza simile per le altre distro, a seconda del sistema di avvio dovrete modificare leggermente lo script.
Come prima cosa abbiamo bisogno di creare un nuovo script. Il seguente è lo script completo utilizzato in questo esempio – spiegazioni seguiranno.
Quindi, in un terminale come utente root digitate:
vi /etc/init.d/mailme |
Come alternativa è possibile utilizzare nano o emacs, l’importante è che si incolli il seguente script nella nuova finestra:
#!/bin/sh # chkconfig: 2345 99 01 # Description: Sends an email at system start and shutdown ############################################# # # # Send an email on system start/stop to # # a user. # # # ############################################# EMAIL="[email protected]" RESTARTSUBJECT="["`hostname`"] - System Startup" SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown" RESTARTBODY="This is an automated message to notify you that "`hostname`" started successfully. Start up Date and Time: "`date` SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down. Shutdown Date and Time: "`date` LOCKFILE=/var/lock/subsys/SystemEmail RETVAL=0 # Source function library. . /etc/init.d/functions stop() { echo -n $"Sending Shutdown Email: " echo "${SHUTDOWNBODY}" | mail -s "${SHUTDOWNSUBJECT}" ${EMAIL} RETVAL=$? if [ ${RETVAL} -eq 0 ]; then rm -f ${LOCKFILE} success else failure fi echo return ${RETVAL} } start() { echo -n $"Sending Startup Email: " echo "${RESTARTBODY}" | mail -s "${RESTARTSUBJECT}" ${EMAIL} RETVAL=$? if [ ${RETVAL} -eq 0 ]; then touch ${LOCKFILE} success else failure fi echo return ${RETVAL} } case $1 in stop) stop ;; start) start ;; *) esac exit ${RETVAL} |
La porzione principale del codice è l’istruzione case in basso. Quando questo script è impostato, ad esso verrà automaticamente passata o “start” o “stop” come parametro. A seconda del valore, l’istruzione case chiamerà la funzione start () o la funzione stop (). Così, ora che abbiamo fatto lo script, abbiamo bisogno di impostarlo in modo che venga eseguito:
Come prima cosa rendiamolo eseguibile:
chmod u+x mailme |
A questo punto è possibile testare il codice eseguendo uno dei due seguenti comandi. Entrambi devono inviare una e-mail, se si modifica la variabile appropriata nello script.
/etc/init.d/mailme start /etc/init.d/mailme stop |
L’ultima cosa che facciamo è impostare la sequenza di boot in modo che sia eseguito automaticamente via chkconfig.
chkconfig mailme on |
Popular Posts:
- Come convertire facilmente i Video di Youtube ad MP3 su GNU/Linux
- (English) Linux shell: Dfc – Check your disk space with style
- Livarp – Una distribuzione Linux molto leggera
- 5 Visualizzatori di fumetti per Linux
- Copy – Un nuovo servizio di disco su Cloud con applicazione per Linux
Find me on Google+

Thank you.
This is very good work,
Thanks, working for me on CentOS. However, in your article you save the file as “emailme” and in the rest of the article you reference to it via “mailme”. And, on our CentOS, I had to install bash mail by entering yum install mails.
Other than that, userful.
Thanks,
name fixed now it’s mailme in the whole document..