我需要有關 bash 和 systemctl 服務的幫助...
我的問題是我的公司要求我自動啟動在我們的 KVM 服務器(CentOS)上運行的所有虛擬機,我們總共有 5 台物理主機;但是,我在其中一個上遇到了一個問題,我最近更新了我的 VM 版本,其中位於/etc/systemd/system下的服務 <vmx.service>沒有運行我的ExecStart=/bin/bash -c '。 /vmx.sh -lv --start --cfg config/pod8/vmx1.conf' bash 腳本。
file: vmx.service
[Unit]
Description=Juniper vMX Router
Wants=network-online.target
After=network-online.target
After=libvirtd.service
[Service]
WorkingDirectory=/home/vMX-21.1R1/
Environment="PATH=/opt/rh/python27/root/usr/bin:/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/$
Type=oneshot
User=root
Group=root
RemainAfterExit=true
## Commands used to stop/start/restart the vMX
ExecStart=/bin/bash -c './path-python.sh'
ExecStart=/bin/bash -c './vmx.sh -lv --start --cfg config/pod8/vmx1.conf' <<<<<<<<<<<<<<<<<<<<<<<
ExecStart=/bin/bash -c './vmx.sh -lv --start --cfg config/pod8/vmx2.conf'
ExecStart=/bin/bash -c './vmx.sh -lv --start --cfg config/pod8/vr-device.conf'
[Install]
WantedBy=multi-user.target
這是我的服務狀態:
Loaded: loaded (/etc/systemd/system/vmx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2021-11-29 14:04:37 AEST; 2h 4min ago
Process: 2324 ExecStart=/bin/bash -c ./vmx.sh -lv --start --cfg config/pod8/vmx1.conf (code=exited, status=2)
Process: 1877 ExecStart=/bin/bash -c ./path-python.sh (code=exited, status=0/SUCCESS)
Main PID: 2324 (code=exited, status=2)
Nov 29 14:04:37 Juniper-KVM3 bash[2324]: import netifaces as ni
Nov 29 14:04:37 Juniper-KVM3 bash[2324]: ImportError: No module named netifaces
Nov 29 14:04:37 Juniper-KVM3 bash[2324]: Log file........................................../home....log
Nov 29 14:04:37 Juniper-KVM3 bash[2324]: ==================================================
Nov 29 14:04:37 Juniper-KVM3 bash[2324]: Aborted!. 1 error(s) and 0 warning(s)
Nov 29 14:04:37 Juniper-KVM3 bash[2324]: ==================================================
Nov 29 14:04:37 Juniper-KVM3 systemd[1]: vmx.service: main process exited, code=exited, status=2...MENT
Nov 29 14:04:37 Juniper-KVM3 systemd[1]: Failed to start Juniper vMX Router.
Nov 29 14:04:37 Juniper-KVM3 systemd[1]: Unit vmx.service entered failed state.
Nov 29 14:04:37 Juniper-KVM3 systemd[1]: vmx.service failed.
我認為錯誤:ImportError: No module named netifaces
將通過pip uninstall netifaces && pip install netifaces
按照本文中閱讀的方式運行來修復。但是,它也不起作用,無論如何,最奇怪的是,當我在終端中運行相同的腳本時,它起作用了:
[root@system]# ./vmx.sh -lv --stop --cfg config/pod8/vmx1.conf
[...]
==================================================
VMX Status Verification Completed.
==================================================
Log file........................................../home/vMX-21.1R1/build/8m1/logs/vmx_1638166233.log
==================================================
Thank you for using VMX
==================================================
我確保禁用了 SElinux:
SELinux status: disabled
值得注意的是./path-python.sh腳本運行沒有問題,這個腳本允許我改變我的python27路徑,否則我的VM安裝會失敗,但我可以肯定地說這個問題與python本身無關,因為該腳本僅在我在終端中運行時才有效。(我的其他服務器使用相同的腳本和 bash 按預期工作,我不知道出了什麼問題)。
所以,在玩弄之後,virtualenv
我終於成功了!以前,運行./vmx.sh腳本時,需要係統使用Python2.7;但是,每次我的主機重新啟動時,如果我想完全自動化所有虛擬機的啟動,我需要通過在我的終端上手動運行以下命令來更改 Python 的 PATH:
echo 'export PATH=/opt/rh/python27/root/usr/bin:$PATH' >> /etc/profile
PATH=/opt/rh/python27/root/usr/bin:$PATH
export PATH
cd /opt/rh/python27/ && . enable && pip install netifaces pyyaml
有人指出,“與 Python 相關的路徑調整將無法用於後續的 ExecStart 命令”,而不是從 .sh 腳本運行先前的命令,該腳本最終對我不起作用。出於這個原因,我做了以下事情:
#!/bin/bash
source scl_source enable python27
我安裝了 virtualenv pip install virtualenv
創建了一個虛擬環境virtualenv --python=/usr/bin/python2.7 vMX-ENV
,我將用它來安裝我所有的包和腳本來安裝我的 VM
激活我的虛擬環境 source /home/vMX-ENV/bin/activate
創建了一個目錄 (vMX-*),./ vmx.sh腳本將位於其中。
# ls
bin lib lib64 pyvenv.cfg vMX-21.1R1
#!/bin/bash
source /home/vMX-ENV/bin/activate
./vmx.sh -lv --start --cfg config/pod17/vmx1.conf
# ...
ExecStart=/bin/bash -c './pyrun.sh'
通過創建一個 virtualenv,我能夠通過只創建一個包含所有需要的包的獨特環境並只運行運行 ./vmx.sh 所需的 Python2.7 來隔離我的 Python 項目
重新啟動主機後,這是journalctl -u vmxd.service
成功的輸出結果:
Starting Juniper vMX Router...
==================================================
Welcome to VMX
==================================================
[...]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句