Productivity is the result of a commitment to excellence, intelligent planning, and focused effort.

Set Environment Variables in LINUX with PM2 for Node and more

Cover Image for Set Environment Variables in LINUX with PM2 for Node and more
George Crisan
George Crisan
Posted on:

Intro

Environment variables are an essential component of any Linux system, enabling the configuration and behavior of various applications and services. In this article, we will explore how to print, set, and use environment variables in Linux, along with a specific focus on PM2's "--update-env" flag for restarting services. Whether you're a beginner or an experienced Linux user, this guide will help you navigate the world of environment variables and enhance your system's functionality.

I. Printing Environment Variables

To print environment variables in Linux, you can make use of the "echo" command followed by the variable name prefixed with a "$" symbol. Open your terminal and type the following command to print the "PATH" variable, which holds the system's executable search paths.


echo $PATH

II. Setting Environment Variables

Linux provides multiple ways to set environment variables, depending on your requirements. Here are three commonly used methods:

Setting Environment Variables in the Current Shell Session

To set an environment variable that will only be active during the current shell session, use the following command:


export VARIABLE_NAME=value

Replace "VARIABLE_NAME" with the name of the environment variable you wish to set, and "value" with the desired value. For example, to set the "MY_NAME" environment variable to "George Crisan", type:


export MY_NAME="George Crisan"

Setting Environment Variables System-Wide

To set an environment variable system-wide, allowing it to be accessible by all users and services, you need to modify the appropriate configuration file. The location of the file depends on the Linux distribution and the shell you are using.

For example, if you're using the Bash shell, open the "~/.bashrc" file in a text editor and add the following line at the end:


export VARIABLE_NAME=value

Save the file and exit. To activate the changes in the current shell session, either restart your shell or run the following command:


source ~/.bashrc

Now, the environment variable will be available to all users and services on your system.

Setting Environment Variables for a Specific Service using PM2

PM2 is a popular process manager for Node.js applications. It provides a convenient way to manage and monitor your services. When using PM2, you can set environment variables specifically for a service using the "--env" flag or update the environment variables on the fly with the "--update-env" flag.

To set environment variables for a specific service, use the following command:


pm2 start app.js --name MyService --env VARIABLE_NAME=value

Replace "app.js" with the entry point of your application, "MyService" with the desired name for your service, and "VARIABLE_NAME" with the name of the environment variable you want to set.

Now, let's explore the "--update-env" flag for updating environment variables when restarting a service:


pm2 restart MyService --update-env

This command will restart the "MyService" service while updating any environment variables specified in the service configuration. It allows you to modify environment variables without restarting the entire PM2 process, making it particularly useful for maintaining continuous service availability.

III. Node

Environment variables are commonly used in shell scripts to control their behavior or store important configuration values. You can reference environment variables within your script using the "$" symbol followed by the variable name.

Applications can make use of environment variables to customize their behavior or access sensitive information such as API keys, database credentials, or configuration settings. Most programming languages provide mechanisms to access environment variables within the code.

For example, in a Node.js application, you can access environment variables using the "process.env" object. Suppose you have an environment variable called "API_KEY" that stores an API key for external services. You can access it in your Node.js application like this:


const apiKey = process.env.API_KEY;

// Use the API key in your code
console.log("API Key:", apiKey);

Conclusion

Environment variables are powerful tools that enable flexible configuration and behavior in Linux systems. In this article, we discussed how to print, set, and use environment variables in Linux. We explored different methods to print variables, set them for the current shell session or system-wide, and specifically for services using PM2. Additionally, we highlighted the "--update-env" flag in PM2, which allows you to update environment variables when restarting a service.

By effectively utilizing environment variables, you can enhance the versatility and portability of your scripts, applications, and configuration files. Embrace the power of environment variables and unlock a new level of flexibility in your Linux environment.

Thank you for following so far.