Skip to content

Commit

Permalink
Merge pull request #2720 from porsager/multi-user-startup
Browse files Browse the repository at this point in the history
Multi user startup - fixes #2662
  • Loading branch information
Unitech committed Feb 21, 2017
2 parents 4e4b498 + aa279ac commit 2a9c347
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion bin/pm2
Expand Up @@ -561,7 +561,7 @@ commander.command('resurrect')
commander.command('unstartup [platform]')
.description('disable and clear auto startup - [platform]=systemd,upstart,launchd,rcd')
.action(function(platform) {
pm2.uninstallStartup(platform);
pm2.uninstallStartup(platform, commander);
});

//
Expand Down
75 changes: 40 additions & 35 deletions lib/API/Startup.js
Expand Up @@ -22,12 +22,12 @@ module.exports = function(CLI) {
function isNotRoot(platform, opts, cb) {
if (opts.user) {
console.log(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:');
console.log('sudo env PATH=$PATH:' + path.dirname(process.execPath) + ' pm2 startup ' + platform + ' -u ' + opts.user + ' --hp ' + process.env.HOME);
console.log('sudo env PATH=$PATH:' + path.dirname(process.execPath) + ' pm2 ' + opts.args[1].name() + ' ' + platform + ' -u ' + opts.user + ' --hp ' + process.env.HOME);
return cb(new Error('You have to run this with elevated rights'));
}
return exec('whoami', function(err, stdout, stderr) {
console.log(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:');
console.log('sudo env PATH=$PATH:' + path.dirname(process.execPath) + ' ' + require.main.filename + ' startup ' + platform + ' -u ' + stdout.trim() + ' --hp ' + process.env.HOME);
console.log('sudo env PATH=$PATH:' + path.dirname(process.execPath) + ' ' + require.main.filename + ' ' + opts.args[1].name() + ' ' + platform + ' -u ' + stdout.trim() + ' --hp ' + process.env.HOME);
return cb(new Error('You have to run this with elevated rights'));
});
}
Expand Down Expand Up @@ -59,10 +59,13 @@ module.exports = function(CLI) {
return hash_map[init_systems[i]];
}

CLI.prototype.uninstallStartup = function(platform, cb) {
CLI.prototype.uninstallStartup = function(platform, opts, cb) {
var commands;
var that = this;
var actual_platform = detectInitSystem();
var user = opts.user || process.env.USER;
var service_name = 'pm2-' + user;
var launchd_service_name = 'pm2.' + user;

if (!platform)
platform = actual_platform;
Expand All @@ -85,8 +88,7 @@ module.exports = function(CLI) {
}

if (process.getuid() != 0) {
console.log('sudo env PATH=$PATH:' + path.dirname(process.execPath) + ' ' + require.main.filename + ' unstartup ' + platform);
return cb();
return isNotRoot(platform, opts, cb);
}

if (fs.existsSync('/etc/init.d/pm2-init.sh')) {
Expand All @@ -96,16 +98,16 @@ module.exports = function(CLI) {
switch(platform) {
case 'systemd':
commands = [
'systemctl stop pm2',
'systemctl disable pm2',
'systemctl status pm2',
'rm /etc/systemd/system/pm2.service'
'systemctl stop ' + service_name,
'systemctl disable ' + service_name,
'systemctl status ' + service_name,
'rm /etc/systemd/system/' + service_name + '.service'
];
break;
case 'systemv':
commands = [
'chkconfig pm2 off',
'rm /etc/init.d/pm2'
'chkconfig ' + service_name + ' off',
'rm /etc/init.d/' + service_name
];
break;
case 'oldsystem':
Expand All @@ -118,22 +120,22 @@ module.exports = function(CLI) {
break;
case 'openrc':
commands = [
'/etc/init.d/pm2 stop',
'rc-update delete pm2 default',
'rm /etc/init.d/pm2'
'/etc/init.d/' + service_name + ' stop',
'rc-update delete ' + service_name + ' default',
'rm /etc/init.d/' + service_name
];
break;
case 'upstart':
commands = [
'update-rc.d pm2 disable',
'update-rc.d -f pm2 remove',
'rm /etc/init.d/pm2'
'update-rc.d ' + service_name + ' disable',
'update-rc.d -f ' + service_name + ' remove',
'rm /etc/init.d/' + service_name
];
break;
case 'launchd':
var destination = path.join(process.env.HOME, 'Library/LaunchAgents/PM2.plist');
var destination = path.join(process.env.HOME, 'Library/LaunchAgents/' + launchd_service_name + '.plist');
commands = [
'launchctl remove com.PM2',
'launchctl remove com.' + launchd_service_name,
'rm ' + destination
];
};
Expand Down Expand Up @@ -162,6 +164,9 @@ module.exports = function(CLI) {
CLI.prototype.startup = function(platform, opts, cb) {
var that = this;
var actual_platform = detectInitSystem();
var user = (opts.user || process.env.USER);
var service_name = 'pm2-' + user;
var launchd_service_name = 'pm2.' + user;

if (!platform)
platform = actual_platform;
Expand Down Expand Up @@ -202,65 +207,65 @@ module.exports = function(CLI) {
case 'oracle':
case 'systemd':
template = getTemplate('systemd');
destination = '/etc/systemd/system/pm2.service';
destination = '/etc/systemd/system/' + service_name + '.service';
commands = [
'chmod +x ' + destination,
'systemctl enable pm2',
'systemctl start pm2',
'systemctl enable ' + service_name,
'systemctl start ' + service_name,
'systemctl daemon-reload',
'systemctl status pm2'
'systemctl status ' + service_name
];
break;
case 'ubuntu14':
case 'ubuntu12':
case 'upstart':
template = getTemplate('upstart');
destination = '/etc/init.d/pm2';
destination = '/etc/init.d/' + service_name;
commands = [
'chmod +x ' + destination,
'mkdir -p /var/lock/subsys',
'touch /var/lock/subsys/pm2',
'update-rc.d pm2 defaults'
'touch /var/lock/subsys/' + service_name,
'update-rc.d ' + service_name + ' defaults'
];
break;
case 'systemv':
case 'amazon':
case 'centos6':
template = getTemplate('upstart');
destination = '/etc/init.d/pm2';
destination = '/etc/init.d/' + service_name;
commands = [
'chmod +x ' + destination,
'mkdir -p /var/lock/subsys',
'touch /var/lock/subsys/pm2',
'chkconfig --add pm2',
'chkconfig pm2 on',
'touch /var/lock/subsys/' + service_name,
'chkconfig --add ' + service_name,
'chkconfig ' + service_name + ' on',
'initctl list'
];
break;
case 'macos':
case 'darwin':
case 'launchd':
template = getTemplate('launchd');
destination = path.join(process.env.HOME, 'Library/LaunchAgents/PM2.plist');
destination = path.join(process.env.HOME, 'Library/LaunchAgents/' + launchd_service_name + '.plist');
commands = [
'launchctl load -w ' + destination
]
break;
case 'freebsd':
case 'rcd':
template = getTemplate('rcd');
destination = '/etc/rc.d/pm2';
destination = '/etc/rc.d/' + service_name;
commands = [
'chmod +x ' + destination,
'echo "pm2_enable=YES" >> /etc/rc.conf'
];
break;
case 'openrc':
template = getTemplate('openrc');
destination = '/etc/init.d/pm2';
destination = '/etc/init.d/' + service_name;
commands = [
'chmod +x ' + destination,
'rc-update add pm2 default'
'rc-update add ' + service_name + ' default'
];
break;
default:
Expand All @@ -272,7 +277,7 @@ module.exports = function(CLI) {
*/
template = template.replace(/%PM2_PATH%/g, process.mainModule.filename)
.replace(/%NODE_PATH%/g, path.dirname(process.execPath))
.replace(/%USER%/g, opts.user || process.env.USER)
.replace(/%USER%/g, user)
.replace(/%HOME_PATH%/g, opts.hp ? path.resolve(opts.hp, '.pm2') : cst.PM2_ROOT_PATH);

console.log(chalk.bold('Platform'), platform);
Expand Down
4 changes: 2 additions & 2 deletions test/bash/startup.sh
Expand Up @@ -13,9 +13,9 @@ cd $file_path

$pm2 startup upstart -u $USER --hp $HOME
spec "should startup command generation exited succesfully"
test -e /etc/init.d/pm2
test -e /etc/init.d/pm2-$USER
spec "should have generated upstart file"
$pm2 unstartup upstart
spec "should have disabled startup"
! test -e /etc/init.d/pm2
! test -e /etc/init.d/pm2-$USER
spec "should have deleted upstart file"

0 comments on commit 2a9c347

Please sign in to comment.