[英]Why bash script, that must creat and run zabbix server, execute without errors, but result is wrong?
每個人。 我ve started to learn bash and DevOps, lost 2 days, but I can
理解以下內容:要求
我需要編寫 bash 腳本(問題末尾的腳本),它創建並運行 zabbix 服務器(正好是 4 版本)。 當我以兩種方式逐步手動運行腳本(手動在 pwsh 中插入每個命令)以創建數據庫(示例 1 或示例 2)時 - 它正確執行:創建 zabbix db,啟動所有服務,zabbix 前端運行良好。 但是當我以 root 用戶身份或通過命令以本地用戶身份運行腳本時:
>sh -c /home/zabbix_server
腳本完成:
>Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
>Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
>Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
在腳本期間或腳本結束時沒有錯誤消息,但是當我嘗試手動執行命令 create db(example1 或 example2)時,沒關系:
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'@'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'@'localhost';
EOF
#Create DB (example2)
mysql -u root -e "CREATE DATABASE zabbix; CREATE USER zabbix@localhost identified by 'zabbix'; GRANT ALL ON zabbix.* to zabbix@localhost WITH GRANT OPTION;"
我收到錯誤消息: ... db can't be created, zabbix base exists 但是命令的結果:
>show databases;
在 db 列表中不顯示 db zabbix。
腳本
#!/usr/bin/env bash
setenforce 0
#Install the repository configuration package
rpm -Uvh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
yum clean all
#Install Zabbix server, frontend, agent, database
yum install zabbix-server-mysql -y
yum install zabbix-web-mysql -y
yum install zabbix-agent -y
yum install mariadb-server -y
#Start DB
systemctl start mariadb
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'@'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'@'localhost';
EOF
#Create DB (example2)
mysql -u root -e "CREATE DATABASE zabbix; CREATE USER zabbix@localhost identified by 'zabbix'; GRANT ALL ON zabbix.* to zabbix@localhost WITH GRANT OPTION;"
#Import initial schema and data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
#Configure the database for Zabbix server
echo DBPassword=zabbix >> /etc/zabbix/zabbix_server.conf
#Start zabbix server
systemctl start zabbix-server
#Configure frontend
sed -i 's:# php_value date.timezone.*:php_value date.timezone Europe\/Minsk:g' /etc/httpd/conf.d/zabbix.conf;
#Start httpd
systemctl restart zabbix-server zabbix-agent httpd
#Make Zabbix server and agent processes start at system boot
systemctl enable zabbix-server zabbix-agent httpd
根據您想要實現的目標,這可能是一個很好的建議:
CREATE DATABASE IF NOT EXISTS zabbix;
這使得調用是冪等的!
問題是,該腳本在末尾 (0d) 包含一個 \r (CR)。 刪除它:
tr -d '\r' < old_name_script > new_name_script
並且腳本運行良好。 如果它有用,有一個完整的腳本示例,它創建了 zabbix 服務器:
#!/usr/bin/env bash
#Disable SELinux
enforceStatus=getenforse
if [ "$enforceStatus" != "Permissive" ]; then
setenforce 0
fi
#Install the repository configuration package
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
#Install Zabbix server, frontend, agent, database, httpd
yum install zabbix-server-mysql -y
yum install zabbix-web-mysql -y
yum install zabbix-agent -y
yum install mariadb mariadb-server -y
yum install httpd -y
#Start and add to autostart DB mariadb
systemctl start mariadb
systemctl enable mariadb.service
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'@'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'@'localhost';
EOF
#Import initial schema and data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
#Configure the database for Zabbix server
echo DBPassword=zabbix >> /etc/zabbix/zabbix_server.conf
#Configure frontend
sed -i 's:# php_value date.timezone.*:php_value date.timezone Europe\/Minsk:g' /etc/httpd/conf.d/zabbix.conf;
#Start zabbix server processes start at system boot
systemctl restart zabbix-server
systemctl enable zabbix-server
#Start httpd processes start at system boot
systemctl restart httpd
systemctl enable httpd
#Start zabbix-agent processes start at system boot
systemctl restart zabbix-agent
systemctl enable zabbix-agent
#Add permissions to irewall
firewall-cmd --permanent --add-port=10050/tcp
firewall-cmd --permanent --add-port=10051/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.