Knowledgebase

Working with Environment Variables and Paths Print

  • vpsservermanagement, cron, troubleshooting, database, security, permissions, guide, howto
  • 0

Why a command works for you and not in a script.

THE PROBLEM

Your interactive shell loads a profile that sets PATH and other variables. Cron jobs and system services do not load the same environment.

A command that works when you type it may not be found when cron runs it.

THE FIX

Use full paths to commands in scripts and cron jobs. Find the path with which.

Set any required variables explicitly within the script.

ENVIRONMENT VARIABLES FOR APPLICATIONS

Applications frequently read configuration from environment variables: database credentials, API keys, the environment name.

Set these in the service configuration or in a file the application reads, not in your interactive profile, or they will be absent when the service starts at boot.

SECURITY

Environment variables holding credentials are readable by anyone who can list processes on some systems. Prefer a configuration file with restrictive permissions where the application supports it.

Never commit a file containing credentials to version control.

DEBUGGING

Print the environment from within the failing context to see what is actually set.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot