Skip to content
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

Adds option to switch to a different user/group before starting a managed process #329

Merged
merged 2 commits into from
May 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ $ pm2 start app.json # Start processes with options declared in a
$ pm2 start app.js -i max -- -a 23 # Pass arguments after -- to app.js

$ pm2 start app.js -i max -e err.log -o out.log # Will start and generate a configuration file

$ pm2 --run-as-user foo start app.js # Start app.js as user foo instead of root (pm2 must be running as root)

$ pm2 --run-as-user foo --run-as-group bar start app.js # Start app.js as foo:bar instead of root:root (pm2 must be running as root)
```

You can also execute app in other languages ([the fork mode](#a23)):
Expand Down
2 changes: 2 additions & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ commander.version(pkg.version)
function(val) {
return val.split(' ');
})
.option('--run-as-user <run_as_user>', 'The user or uid to run a managed process as')
.option('--run-as-group <run_as_group>', 'The group or gid to run a managed process as')
.usage('[cmd] app');

//
Expand Down
4 changes: 4 additions & 0 deletions lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ CLI.startFile = function(script) {
appConf['pid_file'] = commander.pid;
if (commander.cron)
appConf['cron_restart'] = commander.cron;
if (commander.runAsUser)
appConf['run_as_user'] = commander.runAsUser;
if (commander.runAsGroup)
appConf['run_as_group'] = commander.runAsGroup;

if (commander.executeCommand)
appConf['exec_mode'] = 'fork_mode';
Expand Down
10 changes: 10 additions & 0 deletions lib/ProcessContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ function exec(script, outFile, errFile) {
}
});

// if we've been told to run as a different user or group (e.g. because they have fewer
// privileges), switch to that user before importing any third party application code.
if (process.env.run_as_group) {
process.setgid(process.env.run_as_group);
}

if (process.env.run_as_user) {
process.setuid(process.env.run_as_user);
}

// Get the script & exec
require(script);
});
Expand Down