Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Shell Command Patterns for System Administration

Tech 1

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

  1. Edit the hosts file:

    sudo vim /etc/hosts
    
  2. Add the following entry:

    199.232.28.133 raw.githubusercontent.com
    
  3. 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

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.