The day-to-day tasks of the sysadmin are always different for everyone; however, there are simple tasks that are executed equally on managed systems. In the days when disk space was a risk factor in the administrator's day, it was vitally important to locate the directory or filesystem to debug.
Nowadays, in virtual machines mainly, it's just as important to have the managed systems healthy and with space available for the execution of their processes and logging in their log files.
[ You might also like: Linux scripting: 3 how-tos for while loops in Bash ]
The best way to start is to use the simplest commands by linking their outputs as inputs to a new execution, forming what is known as a one-liner. That is, using the command line interpreter to read the standard output of a command and put it as the input variable of the next command, for this, the xargs command helps receive that variable and execute it.
Check out a practical example:
Ticket RH0502201-1: The prodenv production server sends "low disk space on root filesystem" alerts. The filesystem purge is requested.
-
Access the server and check the space in
/filesystem:
# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/prodenv--vg-root 720G 720G 0 100% /
-
Change to the root directory, list the directories, and to calculate the disk space used in each directory, use a
pipeandxargsto send thelscommand output toducommand:
# ls | xargs du -sk
-
With a
pipeagain, use the sort command to list the output from lowest to highest disk space used in the directories:
# ls | xargs du -sk | sort -n
-
For this case, only the directories with the most disk space occupied are needed, so we could limit the list to the last five directories in the list. Using another pipe and the tail command:
# ls | xargs du -sk | sort -n | tail -5
Note: If you want to avoid the error message where the du command could not access, send the standard error output to the /dev/null device:
# ls | xargs du -sk 2> /dev/null | sort -n | tail -5
-
With the list of directories defined, use AWK to create a new sorted list of directories, column two of the original list:
# ls | xargs du -sk 2> /dev/null | sort -n | tail -5 | awk '{ print $2 }'
-
The sorted list is reentered as a variable to
duto show the space used in a humanly readable format. Use anotherpipeandxargs:
# ls | xargs du -sk 2> /dev/null | sort -n | tail -5 | awk '{ print $2 }' | xargs du -sh
-
Validate the feasibility of deleting or compressing the files that are overwhelming the filesystem to free up as much disk space as possible.
Probably it is possible to avoid steps 5 and 6 and execute the cleaning directly; however, in many cases, these directories belong to some application and notify the responsible party of it to perform the cleaning or simply as evidence of the executed process for the issue resolution documentation.
Finally, our one-liner looks like the following:
# ls | xargs du -sk 2> /dev/null | sort -n | tail -5 | awk '{ print $2 }' | xargs du -sh
This one-liner is part of my arsenal used in the day-to-day tasks as a sysadmin, I hope you find it useful. How about you? What one-liners do you have up your sleeve?
[ Get this free ebook: Managing your Kubernetes clusters for dummies. ]
저자 소개
유사한 검색 결과
Slash VM provisioning time on Red Hat Openshift Virtualization using Red Hat Ansible Automation Platform
Red Hat Ansible Automation Platform: Measuring Business Impact with Dashboard and Analytics
Technically Speaking | Taming AI agents with observability
You Can't Automate The Fire | Code Comments
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래