命令行获取本机ip与java进程号兼容写法

yuyu888 于 2025-03-18 发布

前言

换了下基础镜像,从centos7换成alpine3; 结果由于内核不同导致shell命令的展示也不同; 代码里原来的自动获取ip 与java 进程的shell直接失灵

遂写了一套兼容的写法, 可以避免由于系统不同导致的兼容问题

获取本地IP

centos7写法(原)

ifconfig

[root@5a64c59344fd tmp]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 10.0.65.125  netmask 255.255.255.0  broadcast 10.0.65.255
        ether 02:42:0a:00:41:7d  txqueuelen 0  (Ethernet)
        RX packets 1857219  bytes 1869596677 (1.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1625531  bytes 755588840 (720.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.10  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:0a  txqueuelen 0  (Ethernet)
        RX packets 3071823  bytes 3818583903 (3.5 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3901239  bytes 1999734532 (1.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 646986  bytes 49083885 (46.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 646986  bytes 49083885 (46.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@5a64c59344fd tmp]# 

原shell

SERVICE_IP=$(ifconfig | grep "inet" | awk -F "[: ]+" '{print $3}' | head -n1)

这里用 {print $3} 写死了, 用在这个结构下没有问题

alpine3写法

ifconfig

k8s-test-fileservice-7fd5756b46-k5mpj:/# ifconfig
eth0      Link encap:Ethernet  HWaddr E6:FB:E7:59:56:2A  
          inet addr:172.22.0.121  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:85901 errors:0 dropped:0 overruns:0 frame:0
          TX packets:77105 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:8412465 (8.0 MiB)  TX bytes:7646501 (7.2 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:11228 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11228 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:561400 (548.2 KiB)  TX bytes:561400 (548.2 KiB)

k8s-test-fileservice-7fd5756b46-k5mpj:/# 

inet addr:172.22.0.121 这一行多了一个addr: 用原命令得改成print $4;如果基础镜像又切回centos7那么shell也得跟着改, 所以最好有一个兼容的写法

兼容写法

SERVICE_IP=$(ifconfig eth0|awk '{if($0~"inet") {print $0}}'| grep -oE '[0-9]+.[0-9]+.[0-9]+.[0-9]+'|head -n1)

获取java进程号

centos7写法(原)

ps -ef

[root@5a64c59344fd tmp]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Feb25 ?        00:00:00 /bin/bash /home/start_java.sh microservice-file
root         7     1  0 Feb25 ?        00:00:27 sh /home/memory_log.sh
root         8     1  0 Feb25 ?        01:58:48 java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/ -Xmx4096m -XX:MaxDirectMemorySize=2g -jar /tmp/micro
root       330     0  0 Mar03 pts/1    00:00:00 bash
root       831     0  0 17:31 pts/6    00:00:00 bash
root      1334     7  0 17:41 ?        00:00:00 vmstat 5 10
root      1335   831  0 17:41 pts/6    00:00:00 ps -ef
root      2968     0  0 Feb28 pts/0    00:00:00 bash
root     10685     0  0 09:39 pts/5    00:00:00 bash
root     12798     0  0 Mar07 pts/3    00:00:00 bash
root     20904     0  0 Mar03 pts/2    00:00:00 bash
root     30540     0  0 Mar17 pts/4    00:00:00 bash

原shell

ps -ef | grep java |grep microservice  | grep "spring.profiles.active" | awk '{print $2}'

alpine3写法

ps -ef

k8s-test-fileservice-7fd5756b46-k5mpj:/# ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 {start_java.sh} /bin/bash /home/start_java.sh microservice-file
    7 root      1:58 java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/ -Xmx3072m -jar /tmp/microservice-file.jar --spring.profiles.active=test -Dthumbnailator.conserv
 7763 root      0:00 bash
10478 root      0:00 ps -ef
k8s-test-fileservice-7fd5756b46-k5mpj:/# 

这里的PID 变到第一列了

兼容写法

ps -ef | awk 'NR==1 {for(i=1;i<=NF;i++) if ($i=="PID") {pid_col=i}}  NR>1 {if($0!~"ps -ef | awk"  && $0~"microservice" && $0~"spring.profiles.active") {print $pid_col}}'