Skip to content

Environment Variables · Bash Scripts · cron ​

This page puts the whole flow in one place: manage paths with environment variables, collect status with a Bash script (monitor.sh), and run that script automatically with cron.

Environment Variables ​

Concept ​

An environment variable is a key-value pair that a process reads when it runs. If you manage paths with environment variables instead of hardcoding them, the code keeps working without changes even when the environment (development/production) changes.

Basic Usage ​

bash
# Temporary setting (valid only for the current session)
export AGENT_HOME=/home/agent-admin/agent-app

# Check
echo $AGENT_HOME
printenv AGENT_HOME

# List all environment variables
env

Making It Permanent ​

bash
# Apply to a specific user
vi /home/agent-admin/.bashrc

# Add to the end of the file
export AGENT_HOME=/home/agent-admin/agent-app
export AGENT_PORT=15034
export AGENT_UPLOAD_DIR=$AGENT_HOME/upload_files
export AGENT_KEY_PATH=$AGENT_HOME/api_keys/t_secret.key
export AGENT_LOG_DIR=/var/log/agent-app

# Apply
source /home/agent-admin/.bashrc

Bash Shell Scripts ​

Basic Structure ​

bash
#!/bin/bash
# The first line always declares the interpreter (shebang)

# Variable
NAME="agent"
echo "Hello,$NAME"

# Conditional
if [ -z "$PID" ]; then
    echo "No process"
    exit 1
fi

# Store a command's result in a variable
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}')

Common Patterns in monitor.sh ​

bash
# Get the process PID
PID=$(pgrep -f agent_app.py)

# Check that the port is LISTENing
ss -tulnp | grep ":15034" > /dev/null 2>&1

# CPU usage
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)

# Memory usage
MEM=$(free | grep Mem | awk '{printf "%.1f", $3/$2 * 100}')

# Disk usage
DISK=$(df / | tail -1 | awk '{print $5}' | tr -d '%')

# Write a log line
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$TIMESTAMP] PID:$PID CPU:${CPU}% MEM:${MEM}% DISK_USED:${DISK}%" >> /var/log/agent-app/monitor.log

Exit Code Rules ​

CodeMeaning
exit 0Normal exit
exit 1Error exit

When a script ends with exit 1, cron or another script can detect it as a failure.

Automating with cron ​

Concept ​

cron is a scheduler that runs commands or scripts automatically at set times.

Editing the crontab ​

bash
# Edit the crontab as the agent-admin account
crontab -e

# Register
* * * * * /home/agent-admin/agent-app/bin/monitor.sh

# Check
crontab -l

Time Format ​

*  *  *  *  *  command-to-run
|  |  |  |  |
|  |  |  |  +-- day of week (0=Sunday, 6=Saturday)
|  |  |  +----- month (1-12)
|  |  +-------- day of month (1-31)
|  +----------- hour (0-23)
+-------------- minute (0-59)

* = every time (all values)
ExpressionMeaning
* * * * *Run every minute
0 * * * *Run at the top of every hour
0 9 * * 1Run every Monday at 9:00

Summary ​

ItemDetails
Environment variablesexport is valid only for the current session; add it to .bashrc and source it to make it permanent
Bash scriptshebang (#!/bin/bash), variables, conditionals, store command results with $(...)
What monitor.sh collectsPID (pgrep), port (ss), CPU (top), memory (free), disk (df) → monitor.log
Exit codesexit 0 normal, exit 1 error (lets cron and others detect failure)
cronRegister with crontab -e, * * * * * = run every minute, check with crontab -l

Source: Codyssey-B1/B4-1