Master Essential Linux Ops: xargs, Background Jobs, Process Monitoring & More
This guide walks you through practical Linux operations—from using xargs for efficient file handling and running commands in the background, to monitoring high‑memory and high‑CPU processes, viewing multiple logs with multitail, continuous ping logging, checking TCP states, identifying top IPs on port 80, and leveraging SSH for port forwarding.
Preface
After years of struggling with ad‑hoc scripts, the author shares concise explanations of advanced Linux commands to help both newcomers and seasoned operators write cleaner, more efficient scripts.
1. Practical xargs command
The
xargsutility feeds the output of one command as arguments to another, simplifying tasks such as finding all
.conffiles and classifying them.
<code># find / -name *.conf -type f -print | xargs file</code>The result can be piped into other commands, for example to archive the files:
<code># find / -name *.conf -type f -print | xargs tar cjf test.tar.gz</code>2. Running commands or scripts in background
Use
nohupto keep long‑running operations alive after the terminal closes, e.g., exporting all MySQL databases:
<code>nohup mysqldump -uroot -pXXXXX --all-databases > ./alldatabases.sql &</code>If you prefer to enter the password interactively, run the command without
&, then suspend it with
Ctrl+Zand resume in the background with
bg. Output and errors are captured in
nohup.out.
3. Find processes with high memory usage
<code># ps -aux | sort -rnk 4 | head -20</code>The fourth column shows memory‑usage percentage; the last column lists the corresponding process.
4. Find processes with high CPU usage
<code># ps -aux | sort -rnk 3 | head -20</code>The third column indicates CPU‑usage percentage.
5. View multiple logs simultaneously
Install
multitailto monitor several log files in one terminal with highlighting and filtering.
<code># wget ftp://ftp.is.co.za/mirror/ftp.rpmforge.net/redhat/el6/x86_64/dag/RPMS/multitail-5.2.9-1.el6.rf.x86_64.rpm
# yum -y localinstall multitail-5.2.9-1.el6.rf.x86_64.rpm</code>Example: watch
/var/log/securefor the keyword “Accepted” while simultaneously pinging Baidu:
<code># multitail -e "Accepted" /var/log/secure -l "ping baidu.com"</code>6. Continuous ping logging
ping api.jpush.cn | awk '{ print $0 " " strftime("%Y-%m-%d %H:%M:%S",systime()) }' >> /tmp/jiguang.log &
The command appends a timestamped ping line to
/tmp/jiguang.logevery second.
7. Check TCP connection states
<code># netstat -nat | awk '{print $6}' | sort | uniq -c | sort -rn</code>8. Find top 20 IPs requesting port 80
<code># netstat -anlp | grep 80 | grep tcp | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n20</code>9. SSH port forwarding
Forward port 9200 on a bastion host to an internal Elasticsearch server:
<code>ssh -p 22 -C -f -N -g -L 9200:192.168.1.19:9200 [email protected]</code>After executing, accessing
192.168.1.15:9200actually reaches
192.168.1.19:9200. Ensure SSH keys are exchanged beforehand.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.