Shell Command Patterns for System Administration
Automated Service Startup with Password Authentication
The following expect script handles services requiring sudo access:
#!/usr/bin/expect
set timeout 5
spawn mysql.server start
echo "MySQL service started successfully"
spawn sh /opt/app/tomcat/bin/shutdown.sh
spawn sh /opt/app/tomcat/bin/startup.sh
echo "Tomcat service started"
spawn sudo uwsgi --ini /var/www/app/uwsgi.ini
expect "*password:"
send "adminpass\r"
interact
spawn sudo nginx -c /var/www/app/nginx.conf
expect "*password:"
send "adminpass\r"
interact
Execute the script directly without the sh prefix.
Service Management Scripts
Startup Script
#!/bin/bash
echo "Starting Web Application"
cd /opt/project/webapp/
nohup npm run build >> /var/log/app/build.log 2>&1 &
echo "Starting Backend Service"
cd /opt/project/backend/
nohup python3 manage.py runserver 0.0.0.0:8000 >> /var/log/app/backend.log 2>&1 &
echo "Starting Celery Scheduler"
cd /opt/project/backend/
nohup python3 manage.py celery beat -l info >> /var/log/app/beat.log 2>&1 &
echo "Starting Celery Worker"
cd /opt/project/backend/
celery multi start w1 -A backend -l info --logfile=/var/log/app/worker.log 2>&1 &
Shutdown Script
#!/bin/bash
echo "Stopping Django processes"
pids=$(ps aux | grep "python" | grep "runserver" | awk '{print $2}')
for pid in $pids
do
kill -9 $pid
done
echo "Stopping Celery processes"
pids=$(ps aux | grep "celery" | grep "backend" | awk '{print $2}')
for pid in $pids
do
kill -9 $pid
done
Sorting Files and Directroies by Size
To list driectory contents sorted from largest to smallest:
du -sh * | sort -r
This command displays the disk usage of each item in the current directory, ordered by size in descending order.
Configuring Package Manager Access on macOS
-
Edit the hosts file:
sudo vim /etc/hosts -
Add the following entry:
199.232.28.133 raw.githubusercontent.com -
Execute the installation command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Locating and Removing Large Files on Linux
To find files exceeding 1GB in a specific directory and remove them:
find /opt/data/ -type f -size +1G -delete
Alternatively, with xargs:
find /opt/data/ -type f -size +1G | xargs rm -f