Voici un exemple de script init.d Ubuntu : Créez un fichier syslog2mysql dans /etc/init.d/ ayant comme contenu :
#! /bin/sh # Do NOT "set -e" PATH=/usr/sbin:/usr/bin:/sbin:/bin DESC="Fetch queries from syslog-ng to mysql db" NAME=syslog2mysql PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME # Exit if mysql client is not installed [ -x "/usr/bin/mysql" ] || exit 0 # Load the VERBOSE setting and other rcS variables [ -f /etc/default/rcS ] && . /etc/default/rcS # Define LSB log_* functions. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions # # Function that starts the daemon/service # do_start() { [ ! -e /var/log/mysql.pipe ] && echo " (Creating $NAME pipe)." && mkfifo /var/log/mysql.pipe if [ -e $PIDFILE ]; then if ps -p $(cat $PIDFILE) >/dev/null; then echo -n -e " Error: $NAME seems to be already running!" return 1 else rm -f $PIDFILE fi fi { while [ -e /var/log/mysql.pipe ] do mysql -u syslogfeeder --password=PASS_HERE syslog < /var/log/mysql.pipe >/dev/null sleep 1 done } & echo $! > $PIDFILE } # # Function that stops the daemon/service # do_stop() { if [ -e $PIDFILE ]; then PID=$(cat $PIDFILE) if ps -p $PID >/dev/null; then # get PID of child CPID=$(pgrep -P $PID) # kill script kill $PID # kill child kill $CPID rm -f $PIDFILE return 0 else echo -e " Warning: $NAME was not running." echo -n -e " Cleaning PID file" rm -f $PIDFILE return 1 fi else echo -n -e " Warning: $NAME was not running" return 1 fi } case "$1" in start) [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" do_start case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; stop) [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" do_stop case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; restart|force-reload) # # If the "reload" option is implemented then remove the # 'force-reload' alias # log_daemon_msg "Restarting $DESC" "$NAME" do_stop case "$?" in 0|1) do_start case "$?" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; # Old process is still running *) log_end_msg 1 ;; # Failed to start esac ;; *) # Failed to stop log_end_msg 1 ;; esac ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac :
Créez le lien pour lancer le script au démarrage
sudo update-rc.d syslog2mysql defaults
Maintenant démarrez le service syslog2mysql :
sudo /etc/init.d/syslog2mysql start
ou si vous préférez ne pas utiliser init.d et utiliser directement le script :
sudo ./syslog2mysql.sh &
Source de l'exemple: syslog-ng