unable to use watch with kubectl and xargs - bash

Team, I am trying to run the below every 5seconds but no luck.
watch -n 5 kubectl get pods -n testes-asdf-234-asdf | grep -v Comple | xargs -l1 -- sh -c 'kubectl logs $1 -n testes-asdf-234-asdf main --tail=15' --
output:
does nothing
tried using quotes and command execution is messed up not yielding right output. my command works perfectly when used without watch and quotes
watch "kubectl get pods --no-headers -n testes-asdf-234-asdf | grep -v Comple | xargs -l1 -- sh -c 'kubectl logs $1 -n testes-asdf-234-asdf main --tail=15' --"
Every 2.0s: kubectl get pods --no-headers -n testes-asdf-234-asdf | grep -v Comple | xargs -l1 -- sh -c 'kubectl logs -n testes... Wed Dec 4 10:10:18 2019
Error from server (NotFound): pods "main" not found
Error from server (NotFound): pods "main" not found
Error from server (NotFound): pods "main" not found
Error from server (NotFound): pods "main" not found
Error from server (NotFound): pods "main" not found
Without WATCH runs fine
└─ $ ▶ kubectl get pods --no-headers -n testes-asdf-234-asdf | grep -v Comple | xargs -l1 -- sh -c 'kubectl logs $1 -n testes-asdf-234-asdf main --tail=15' --
**** Stack trace ****
/roadrunner/roadrunner: dw::core::Exception::Exception(dwStatus, char const*)+0xa1 [0xcdfc31]
/roadrunner/roadrunner: dw::roadcast::RoadCastServerEngine::checkMessageEnabledStatus(dwRoadCastMessageType, char const*)+0x3a [0x1c0e1ea]
/roadrunner/roadrunner() [0x1bc2f2a]
/roadrunner/roadrunner: dwRoadCastServer_sendImage()+0x72 [0x1bc4302]
/roadrunner/roadrunner: roadrunner::castData(dwImageObject*, char const*, long, long, unsigned long)+0x66 [0xd03726]
/roadrunner/roadrunner: roadrunner::RRMainPipeline::getCameraEventFrames(roadrunner::CameraFrame**, unsigned int&, roadrunner::RRMainPipeline::CameraFrameList&, dwSensorEvent const*)+0x75c [0xb2c91c]
/roadrunner/roadrunner: roadrunner::RRMainPipeline::getCameraFrames(roadrunner::RRMainPipeline::CameraFrameList*&, roadrunner::CameraFrame**, unsigned int&)+0x3b9 [0xb31039]
/roadrunner/roadrunner: roadrunner::RRMainPipeline::cameraThreadLoop()+0x634 [0xb44b64]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: ()+0xbd8f0 [0x7f8fe0a6f8f0]
/lib/x86_64-linux-gnu/libpthread.so.0: ()+0x76ba [0x7f90050eb6ba]
/lib/x86_64-linux-gnu/libc.so.6: clone()+0x6d [0x7f8fdff9e41d]

Unless you put your command in quotes, bash will try to pass the output of watch ... kubectl ... to the first grep, but watch will never exit.
So try this:
watch -n 5 "kubectl get pods -n test | grep -v Comple | xargs -l1 -- sh -c 'kubectl logs \$1 -n test main --tail=15' --"
EDIT:
Didn't notice you had a $1 in there. You need to escape the $, as it's now enclosed in double quotes and it will just be expanded to an empty string (as per the output you've shown).
I edited the command above to reflect that.

Related

Get logs from all pods in namespace using xargs

Is there anyway to get all logs from pods in a specific namespace running a dynamic command like a combination of awk and xargs?
kubectl get pods | grep Running | awk '{print $1}' | xargs kubectl logs | grep value
I have tried the command above but it's failing like kubectl logs is missing pod name:
error: expected 'logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]'.
POD or TYPE/NAME is a required argument for the logs command
See 'kubectl logs -h' for help and examples
Do you have any suggestion about how get all logs from Running pods?
Think about what your pipeline is doing:
The kubectl logs command takes as an argument a single pod name, but through your use of xargs you're passing it multiple pod names. Make liberal use of the echo command to debug your pipelines; if I have these pods in my current namespace:
$ kubectl get pods -o custom-columns=name:.metadata.name
name
c069609c6193930cd1182e1936d8f0aebf72bc22265099c6a4af791cd2zkt8r
catalog-operator-6b8c45596c-262w9
olm-operator-56cf65dbf9-qwkjh
operatorhubio-catalog-48kgv
packageserver-54878d5cbb-flv2z
packageserver-54878d5cbb-t9tgr
Then running this command:
kubectl get pods | grep Running | awk '{print $1}' | xargs echo kubectl logs
Produces:
kubectl logs catalog-operator-6b8c45596c-262w9 olm-operator-56cf65dbf9-qwkjh operatorhubio-catalog-48kgv packageserver-54878d5cbb-flv2z packageserver-54878d5cbb-t9tgr
To do what you want, you need to arrange to call kubectl logs multiple times with a single argument. You can do that by adding -n1 to your xargs command line. Keeping the echo command, running this:
kubectl get pods | grep Running | awk '{print $1}' | xargs -n1 echo kubectl logs
Gets us:
kubectl logs catalog-operator-6b8c45596c-262w9
kubectl logs olm-operator-56cf65dbf9-qwkjh
kubectl logs operatorhubio-catalog-48kgv
kubectl logs packageserver-54878d5cbb-flv2z
kubectl logs packageserver-54878d5cbb-t9tgr
That looks more reasonable. If we drop the echo and run:
kubectl get pods | grep Running | awk '{print $1}' | xargs -n1 kubectl logs | grep value
Then you will get the result you want. You may want to add the --prefix argument to kubectl logs so that you know which pod generated the match:
kubectl get pods | grep Running | awk '{print $1}' | xargs -n1 kubectl logs --prefix | grep value
Not directly related to your question, but you can lose that grep:
kubectl get pods | awk '/Running/ {print $1}' | xargs -n1 kubectl logs --prefix | grep value
And even lose the awk:
kubectl get pods --field-selector=status.phase==Running -o name | xargs -n1 kubectl logs --prefix | grep value

grep for pods and select each result and pass a command in loop

I have few pods running in my kubernetes cluster. I am developing a shell script and I want to grep for few pods and want to select each pod from the grep result to execute a command.
Lets say I grep few pods by command :
kubectl get pods | grep test
the results are:
Test-0
Test-1
Test-2
From the result, I want to select each pod and execute a command for it in a loop.
for example:
for first pod:
kubectl exec -it Test-0 -- mysqldump.......
after finishing the first pod, it has to process the second pod and so on
for pod in $(kubectl get pod -oname |grep -i Test ); do
kubectl exec "$pod" -- ls -ltr ;
done
Replace ls -ltr with mysqldump .....
Get pods name and then use "for" to execute command in each pod
#!/bin/bash
pods=$(kubectl get pods | awk '{print $2}' | grep -i test)
for i in $pods
do
kubectl exec -it $i -- echo "test"
done
Select your target pods using labels is less error prone and can do multiple matching:
kubectl get pods --selector <key>=<value>,<key>=<value> --namespace <name> -oname | xargs -I{} kubectl exec -it {} --namespace <name> -- mysqldump ...

How to run a command like xargs on a grep output of a pipe of a previous xargs from a command in Bash

I'm trying to understand what's happening here out of curiosity, even though I can just copy and paste the output of the terminal to do what I need to do. The following command does not print anything.
ls /opt/local/var/macports/registry/portfiles -1 | sed 's/-.*//g' | sort -u | parallel "sudo port -N install" {} 2>&1 | grep -Po "Use '\K.*(?=')" | parallel "{}"
The directory I call ls on contains a bunch of filenames starting with the string I want to extract that ends at the first dash (so stringexample-4.2009 pipes stringexample into parallel (like xargs but to run each line separately). After running the command sudo port install <stringexample>, I get error outputs like so:
Unable to activate port <stringexample>. Use 'port -f activate <stringexample>' to force the activation.
Now, I wish to run port -f activate <stringexample>. However, I cannot seem to do anything with the output port -f activate gettext that I get to the terminal.
I cannot even do ... | grep -Po "Use '\K.*(?=')" | xargs echo or ... | grep -Po "Use '\K.*(?=')" >> commands_to_run.txt (the output stream to file only creates an empty file), despite the shorter part of the command:
ls /opt/local/var/macports/registry/portfiles -1 | sed 's/-.*//g' | sort -u | parallel "sudo port -N install {}" 2>&1 | grep -Po "Use '\K.*(?=')"
printing the commands to the terminal. Why does the pipe operator not work here? If the commands I wish to run are outputting to the terminal, surely there's got to be a way to capture them.

How do I pass multiple query parameters by xargs into httpie?

I tried below to pass a parameters to httpie and it turned into POST method unexpectedly.
1)
$ echo "a1 b1" | xargs -t -n2 bash -c 'http -v https://httpbin.org/anything arg1==$0 arg2==$1'
bash -c http -v https://httpbin.org/anything arg1==$0 arg2==$1 a1 b1
2)
$ echo "arg1==a1 arg2==b1" | xargs -t -n2 bash -c 'http -v https://httpbin.org/anything'
bash -c http -v https://httpbin.org/anything arg1==a1 arg2==b1
The 1st one returns below and seem like there're additional "a1 b1" inhibit proper request.
bash -c http -v https://httpbin.org/anything arg1==$0 arg2==$1 a1 b1
The 2nd one returns seemingly not too far but actual method turned into the POST.
Is there any way to pass multiple parameters to httpie?
Here is a way to accomplish your goal:
echo "a1 b1" |
awk '{print "http -v https://httpbin.org/anything arg1=="$1" arg2=="$2}' |
bash
Even if manually insert the strings like:
$ echo 'http -v https://httpbin.org/anything arg1==a1 arg2==b2' | bash
doesn't work same as below:
$ http -v https://httpbin.org/anything arg1==a1 arg2==b2
I don't get the cause of this happening but simply if I specify the method, It worked.
$ echo "a1 b1" | xargs -t -n2 bash -c 'http -v GET https://httpbin.org/anything arg1==$0 arg2==$1
^^^
and I think I got the caused it's due to stdin so it can be avoid by --ignore-stdin option.

Killing Process on Remote Host using Fabric

I am writing a script using Fabric which needs to terminate a process remotely.
(this means that the command ends up getting executed as /bin/bash command)
The current code I have is the following:
in a kill.sh file i have
/bin/kill $(ps -ef | grep multiserver.jar | grep -v bin/sh | grep -v /bin/bash | grep -v sh | grep python | grep -v /usr/bin/java | grep -v /usr/bin/python | grep -v sh | awk '{print $2}')
which I run in Fabric on my remote host using the following commands
local("scp " + "kill.sh " + user +"#" + server_address + ":" + directory)
run ("chmod u+x kill.sh")
run("./kill.sh")
However I get the following error message
out: Usage:
[] out: kill [options] <pid> [...]
Fatal error: run() received nonzero return code 1 while executing!
Requested: ./kill.sh
Executed: /bin/bash -l -c "cd ... && ./kill.sh"
Does anyone know what I am doing wrong?
While solving this issue with reading logs with fabric, I wrote command to kill remote processes:
with settings(warn_only=True):
sudo("kill -9 `ps aux | <pipeline of greps> | awk '{print $2}'`")
Hope this helps.

Resources