Run user script when launch EC2. The commands below will do the following
Create a service user named prometheus
Create a log file owned by the prometheus user to be used by the node_exporter service
Create a script in the /etc/init.d directory to allow running the node_exporter executable as a service
Download version 0.16.0 of node_exporter and untar it into the /home/prometheus/node_exporter directory
Set the node_exporter service to automatically start on boot
Start the node_exporter service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #!/bin/bash sudo useradd --shell /bin/false prometheus sudo mkdir /var/log /prometheus/ sudo touch /var/log /prometheus/node_exporter.log sudo chown -R prometheus:prometheus /var/log /prometheus sudo cat > initscript <<'endmsg' #!/bin/bash . /etc/rc.d/init.d/functions PROGNAME=node_exporter PROGDIR=/home/prometheus/node_exporter PROG=$PROGDIR /$PROGNAME USER=prometheus LOGFILE=/var/log /prometheus/$PROGNAME .log LOCKFILE=/var/run/$PROGNAME .pid start () {echo -n "Starting $PROGNAME : " cd $PROGDIR daemon --user $USER --pidfile="$LOCKFILE " "$PROG &>$LOGFILE &" echo $(pidofproc $PROGNAME ) >$LOCKFILE echo } stop () {echo -n "Shutting down $PROGNAME : " killproc $PROGNAME rm -f $LOCKFILE echo } case "$1 " in start) start ;; stop) stop ;; status) status $PROGNAME ;; restart) stop start ;; reload) echo "Sending SIGHUP to $PROGNAME " kill -SIGHUP $(pidofproc $PROGNAME );; *) echo "Usage: service $PROGNAME {start|stop|status|reload|restart}" exit 1;; esac endmsg sudo mv initscript /etc/init.d/node_exporter sudo chmod +x /etc/init.d/node_exporter sudo chown prometheus:prometheus /etc/init.d/node_exporter curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz sudo mkdir /home/prometheus/node_exporter sudo tar -xf node_exporter-*.tar.gz sudo mv node_exporter-*/* /home/prometheus/node_exporter/ sudo chown -R prometheus:prometheus /home/prometheus/node_exporter sudo chkconfig --add node_exporter sudo service node_exporter start
Đây là Blog cá nhân của MinhHungTrinh, nơi mình chia sẻ, lưu giữ kiến thức. Nếu các bạn có góp ý, thắc mắc thì vui lòng comment bên dưới cho mình biết nhé. Mình luôn là người lắng nghe và ham học hỏi. Các vấn đề đặc biệt hoặc tế nhị mọi người có thể gửi email tới minhhungtrinhvn@gmail.com. Cảm ơn Mọi Người đã đọc Blog của mình. Yolo!