,ps -ef | grep $1 | grep -v grep | grep $2 | grep -v grep | awk '{print $$2}'然后重启zabbix agentd。
实际上只是将awk中的print $2改为print $$2,因为awk正常情况下是不会使用$$2的,我本着疑人不用用人不疑的态度按照邓工的方法尝试,果然奏效!随后邓工告诉我原因,说这个(UserParameter中的Shell语法)跟平常所提到的shell不太相同,问题就出在awk的两个$上。
原来,想要在zabbix里使用shell位置参数,需要使用$$,不能是$,特别是awk中的位置参数,官方原文如下:
To use positional references unaltered, specify double dollar sign - for example, awk '{print $$2}'. In this case $$2 will actually turn into $2 when executing the command.
Note that positional references with the $ sign are interpreted by Zabbix agent regardless of whether they are enclosed between double (“) or single (') quotes.
因此在使用awk处理数据时就注意一下,如果感觉不爽,可以考虑使用cut -f1这样的语法来代替awk。
--end--