① 创建并进入编辑
vi /usr/lib/systemd/system/rocketmq_exporter.service
② 复制下面内容至rocketmq_exporter.service,保存退出(ansible脚本templates需要这个模板文件)
将namesrvAddr 配置成变量替换
[Unit]
Description=rocketmq-exporter
After=network.target
[Service]
Type=forking
User=root
ExecStart=/root/java/jdk1.8.0_181/bin/java -jar /root/rocketmq-exporter/rocketmq-exporter-0.0.2-SNAPSHOT-exec.jar --rocketmq.config.namesrvAddr=192.168.244.133:9876
ExecStop=kill -9 pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
加入需要运行脚本 设置jdk环境,运行sh脚本的参考以下方式:
rocketmq_exporter.service
[Unit]
Description=rocketmq-exporter
After=network.target
[Service]
Type=forking
User=root
ExecStart=/root/rocketmq_exporter_start.sh
ExecStop=kill -9 pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
/root/rocketmq_exporter_start.sh
#!/bin/bash
echo ---------- rocketmq exporter start ------------
export JAVA_HOME=/root/java/jdk1.8.0_181
java -jar /root/rocketmq-exporter/rocketmq-exporter-0.0.2-SNAPSHOT-exec.jar --rocketmq.config.namesrvAddr=192.168.244.133:9876
sleep 3s
echo 'start rocketmq exporter ok'
以下步骤可以写成shell脚本
#!/bin/bash
① 重新加载配置文件
$ systemctl daemon-reload
② 启动服务
$ systemctl start rocketmq_exporter
③ 设置服务开机自启动
$ systemctl enable rocketmq_exporter --now