kubectl exec less command and the output cannot be redirected

9/8/2021

I defined some commands in /etc/profile:

alias umcd="kubectl exec -ti -n actiontech-dmp \
$(k get pods| awk '/'umc'/{print$1}')  \
-c umc -- less /var/log/actiontech/umc/detail.log"

This single alias command works fine, I can see the detail log. However, whenever I run this command:

umcd | grep "some key word" or 
umcd > detail.log. 

The command line tool just hangs up forever... 😭

What is happening? I don't have a single clue about it and it's sad that google doesn't help.

-- Steve Wu
alias
kubernetes
linux

1 Answer

9/8/2021

It's happening because you are using less command which is file viewer - it is user interface tool, it is changing terminal behaviour, while cat just reads data from file and outputs it to the terminal.

less command is too much complex tool for your use and it is not recommended to use it for simple redirection or for matching patterns using grep because it may cause issues like that.

In your case, cat command is right tool. Just change your alias to:

alias umcd="kubectl exec -ti -n actiontech-dmp \
$(k get pods| awk '/'umc'/{print$1}')  \
-c umc -- cat /var/log/actiontech/umc/detail.log"

It will work properly.

-- Mikolaj S.
Source: StackOverflow