Shell & Environment Hygiene
Why Environment Hygiene Matters
In development, environment variables are convenient. In production, they are configuration boundaries. Improper environment management leads to inconsistent behavior, security leaks, and debugging nightmares.
Interactive vs Non-Interactive Shell
Most production services do NOT run inside your interactive shell. They do not load:
- ~/.bashrc
- ~/.profile
- ~/.bash_aliases
systemd services and cron jobs run in controlled, minimal environments. Never assume your shell environment exists in production services.
PATH Risks
The PATH variable determines which binary is executed. Check it:
echo $PATH
Production rule:
- Avoid custom PATH modifications globally
- Never include writable directories in PATH
- Prefer absolute paths in systemd and sudo rules
Absolute Paths Over Assumptions
Instead of:
node app.js
Use:
/usr/bin/node app.js
This prevents execution ambiguity.
systemd Environment Strategy
For production services, use:
Environment="NODE_ENV=production" EnvironmentFile=/etc/myapp.env
Keep environment files:
- Outside application directory
- Readable only by service user
- Not world-readable
Cron Environment Pitfall
Cron runs with a minimal environment. If your script works manually but fails in cron, check:
env
Explicitly define PATH and required variables inside cron jobs.
Environment Variable Security
Environment variables may contain:
- Database passwords
- API keys
- Secrets
Never:
- Expose them via debug endpoints
- Store them in world-readable files
- Echo them in logs
Debugging Environment Drift
printenv systemctl show myapp | grep Environment cat /proc/<pid>/environ
Compare expected vs actual environment.
Common Production Mistakes
- Relying on ~/.bashrc for service variables
- Setting global PATH modifications
- Putting secrets in git repository
- Using different NODE_ENV values across environments
Mental Model
Environment variables are configuration contracts. If they differ between servers, behavior differs between servers. Production reliability depends on environment consistency.
Production Checklist
- All services define environment explicitly
- No reliance on interactive shell configs
- Secrets stored in restricted files
- Absolute paths used in service definitions
- Cron jobs define required environment