top -bn1 | grep "Cpu(s)" | awk '{print "CPU: " $2 "%"}'
top -bn1
│
├── top → shows running processes and system stats
├── -b → batch mode — runs top non-interactively
│ without -b, top opens as live screen
│ with -b, top prints output and exits
└── -n1 → run only 1 iteration then stop
without -n1, top keeps refreshing forever
output looks like:
%Cpu(s): 5.2 us, 1.3 sy, 0.0 ni, 92.8 id
| grep "Cpu(s)"
│
└── filters only the CPU line from top output
ignores all other lines like process list, memory etc
we only want the line that has CPU stats
| awk '{print "CPU: " $2 "%"}'
│
├── awk → text processing tool, splits line into fields
├── $2 → second field of that line
│ %Cpu(s): 5.2 us ...
│ $1 = %Cpu(s):
│ $2 = 5.2 ← this is user CPU usage
│ $3 = us
└── print → prints "CPU: 5.2%"
free -h
│
├── free → shows memory statistics
└── -h → human readable format (MB, GB instead of bytes)
output looks like:
total used free
Mem: 7.7Gi 3.2Gi 4.5Gi
Swap: 2.0Gi 0.0Gi 2.0Gi
| awk 'NR==2 {print "Used: " $3 " / Total: " $2}'
│
├── NR==2 → NR = Number of Row
│ NR==2 means process only row 2
│ row 1 = header (total used free)
│ row 2 = Mem: ← we want this
│ row 3 = Swap: ← skip this
│
├── $2 → second field = total memory (7.7Gi)
├── $3 → third field = used memory (3.2Gi)
└── print → prints "Used: 3.2Gi / Total: 7.7Gi"
In this script if the servers ip is wrong at this point it keeps waiting and wont proceed with checking the other servers. Hence we have added the connection timeout
ssh ec2-user@"$server" "df -hT"
ssh -o ConnectTimeout=5 ec2-user@"$server" "df -hT"
/etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
copytruncate
}
copytruncate: Copies current log Empties original log file
logrotate -d /etc/logrotate.d/myapp -debug mode
logrotate -f /etc/logrotate.d/myapp -force rotation
After log rotation and compression, we can upload old .gz log files to Amazon S3 using AWS CLI.
#!/bin/bash
LOG_PATH="/var/log/myapp"
S3_BUCKET="s3://my-log-backup-bucket"
find $LOG_PATH -name "*.gz" -mtime +1 -exec aws s3 mv {} $S3_BUCKET \;
we can define cronjob for this
0 1 * * * /opt/scripts/log_backup.sh
Runs daily at 1 AM