Shell Scripting Examples
Shell Scripting Examples
list_services.sh
#!/bin/bash
echo "-----------------------------------------------------------"
else
fi
Make it executable
chmod +x list_services.sh
./kill_process.sh list_services.sh
kill_process.sh
#!/bin/bash
if [ $# -eq 0 ]; then
exit 1
fi
process_name=$1
pid=$(pgrep -f "$process_name")
if [ -z "$pid" ]; then
echo "Process '$process_name' not found."
exit 1
fi
kill $pid
if [ $? -eq 0 ]; then
else
exit 1
fi
Make it executable
chmod +x kill_process.sh
./kill_process.sh process_name
3)Need to get the disk space and memory space of the system
system_info.sh
#!/bin/bash
df -h
free -h
Ubuntu:
list_installed_software.sh
#!/bin/bash
dpkg --get-selections
Make it executable
chmod +x list_installed_software.sh
./list_installed_software.sh
CentOS:
list_installed_software.sh
#!/bin/bash
rpm -qa
Make it executable
chmod +x list_installed_software.sh
./list_installed_software.sh
5)To get the service name and stop and start the service like (HTTPD & Nginx & Apache
& Docker)
manage_service.sh
#!/bin/bash
check_status() {
sudo systemctl is-active --quiet $1 && echo "$1 is running" || echo "$1 is not running"
start_service() {
if [ $# -lt 2 ]; then
exit 1
fi
ACTION=$1
SERVICE=$2
case $ACTION in
start)
start_service $SERVICE
;;
stop)
stop_service $SERVICE
;;
status)
check_status $SERVICE
;;
*)
exit 1
;;
esac
chmod +x manage_service.sh
Run the script
6)To list down the agent and if agent is stopped state, then start the service and check
for every time if its stop script must start the service
manage_agents.sh
#!/bin/bash
start_service() {
local service_name=$1
if [ $? -eq 0 ]; then
else
fi
check_and_start_service() {
local service_name=$1
start_service $service_name
else
fi
check_and_start_service $service
done
chmod +x manage_agents.sh
./manage_agents.sh
crontab -e
*/5 * * * * /path/to/manage_agents.sh
7)To check the password expiry of the list of created users and if the password is
expiring in 3 days, then update the password age for next 15 days
check_password_expiry.sh
#!/bin/bash
update_password_age() {
local user=$1
current_date=$(date +%s)
update_password_age $user
else
fi
done
8)To check the particular mount point if it’s reached the 70% utilization then move zip
the file which is older than 7 days and move those files into /tmp/ directory.
script.sh
#!/bin/bash
# Variables
TMP_DIR="/tmp"
find "$TARGET_DIR" -type f -mtime +7 -print0 | while IFS= read -r -d '' file; do
zip_file="${file}.zip"
done
else
fi
9)If the particular mount point is reached the 70 % then delete the older files starting
7 days of files
cleanup.sh
#!/bin/bash
MOUNT_POINT="/path/to/mount"
THRESHOLD=70
# Directory to clean up
DIR_TO_CLEAN="/path/to/directory"
echo "Disk usage is $USAGE%, which is greater than or equal to the threshold of
$THRESHOLD%."
else
fi
10)Every day in the morning at 9am IST and Evening 7 PM IST I need to check the disk
space and free memory and need to run the script and make a cron job to it and store
the output in /tmp directory as diskspace.txt and process.txt
check_system.sh
#!/bin/bash
DISKSPACE_FILE="/tmp/diskspace.txt"
PROCESS_FILE="/tmp/process.txt"
df -h > "$DISKSPACE_FILE"
crontab -e
0 3 * * * /path/to/check_system.sh
0 13 * * * /path/to/check_system.sh