我试图得到一个特定的模式,但是我得到了所有可能的解决方案的结果
ps -eo pid,cmd | grep "qemu.*-name lubuntu" | grep -v grep | awk '{print $1}'
我确实尝试了所有可用的解决方案here
ps -eo pid,cmd | grep "qemu.*-name lubuntu" | grep -v grep
451 qemu-system-x86_64 -enable-kvm -name lubuntu-clone -S -machine pc-i440fx-trusty,accel=kvm,usb=off -cpu SandyBridge,+osxsave,+pcid,+pdcm,+xtpr,+tm2,+est,+smx,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme -m 2048 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid a0b6cbec-3223-fa5e-c6d6-ab88d5736dac -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/lubuntu-clone.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/var/lib/libvirt/images/lubuntu-clone.img,if=none,id=drive-ide0-0-0,format=raw -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -drive if=none,id=drive-ide0-1-0,readonly=on,format=raw -device ide-cd,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=26,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:82:d8:6b,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -vnc 127.0.0.1:0 -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5
467 qemu-system-x86_64 -enable-kvm -name lubuntu -S -machine pc-i440fx-trusty,accel=kvm,usb=off -cpu SandyBridge,+osxsave,+pcid,+pdcm,+xtpr,+tm2,+est,+smx,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme -m 2048 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 9d9b5a31-53ce-113b-9a1c-cc0308fcb174 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/lubuntu.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot strict=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/var/lib/libvirt/images/lubuntu.img,if=none,id=drive-ide0-0-0,format=raw -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -drive if=none,id=drive-ide0-1-0,readonly=on,format=raw -device ide-cd,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=26,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:43:ed:de,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -vnc 127.0.0.1:1 -device cirrus-vga,id=video0,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5
我怎么才能得到
PID: 467
? 最佳答案
如果您的目标是只为“lubuntu”而不是“lubuntu somethingelse”创建grep,那么您可以在之后添加一个空格:
ps -eo pid,cmd | grep "qemu.*-name lubuntu " | grep -v grep | awk '{print $1}'
或者没有额外的命令:
pgrep -f "qemu.*-name lubuntu "
编辑:如果你需要一些可靠的通用的东西,而不是你自己的脚本,那么解析命令行总是一个坏主意。有太多事情会出错。因为您使用libvirt来管理vm,所以请看一下脚本
virsh
。edit2:因此您可以通过从
pid
中的domstatus
节点获取/var/run/libvirt/qemu/lubuntu.xml
属性来获取pid,或者通过virsh desc --live lubuntu
获取相同的信息。关于regex - 如何grep特定模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31400894/