Skip to content

Added a short section about "Setting Environment Variables for Processes" #10983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/process.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ environment variables using the second argument of the ``run()``,
// On both Unix-like and Windows
$process->run(null, ['MESSAGE' => 'Something to output']);

Setting Environment Variables for Processes
-------------------------------------------

The constructor of the :class:`Symfony\\Component\\Process\\Process` class and
all its methods related to executing processes (``run()``, ``mustRun()``,
``start()``, etc.) allow passing an array of environment variables to set before
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``start()``, etc.) allow passing an array of environment variables to set before
``start()``, etc.), allow passing an array of environment variables to set before

running the process::

$process = new Process(['...'], null, ['ENV_VAR_NAME' => 'value']);
$process = Process::fromShellCommandline('...', null, ['ENV_VAR_NAME' => 'value']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need both lines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we're trying to display multiple different ways to set up env vars. Thats's why we have several lines.

$process->run(null, ['ENV_VAR_NAME' => 'value']);
// ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this is only noise


These environment variables are automatically inherited by child processes. You
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These environment variables and the already existing environment variables bound to the current process are inherited by child processes. You can hide an environment variable from child processes by setting it to false:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need some help trying to understand this in practice. Imagine this command:

$process = new Process(['foo'], null, ['APP_ENV' => 'test']);

foo command is run with APP_ENV=test and if foo starts new processes or runs commands, they will use APP_ENV=test too. OK.

But here we are saying, if you want to prevent that, set the env var to false:

$process = new Process(['foo'], null, ['APP_ENV' => false]);

OK, so child processes now don't use APP_ENV=test ... but the main foo command doesn't either, right?

Question: how can I define an env var for a command ... and prevent defining it for its child processes? Thanks.

Copy link
Member

@nicolas-grekas nicolas-grekas Feb 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A running Process instance is a child process. We don't make a difference between a child process and its sub-children. That's not our responsibility at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I understand. Thanks!

can prevent that setting the following env vars to ``false``::

// ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this is only noise

$process = new Process(['...'], null, [
'APP_ENV' => false,
'SYMFONY_DOTENV_VARS' => false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you keep this, you should add this:

Suggested change
'SYMFONY_DOTENV_VARS' => false
'SYMFONY_DOTENV_VARS' => false,

]);

Getting real-time Process Output
--------------------------------

Expand Down