* Restart a process with the same full path * Or start it
(cb)
| 817 | * Or start it |
| 818 | */ |
| 819 | function restartExistingProcessPathOrStartNew(cb) { |
| 820 | that.Client.executeRemote('getMonitorData', {}, function(err, procs) { |
| 821 | if (err) return cb ? cb(new Error(err)) : that.exitCli(conf.ERROR_EXIT); |
| 822 | |
| 823 | var full_path = path.resolve(that.cwd, script); |
| 824 | var managed_script = null; |
| 825 | |
| 826 | procs.forEach(function(proc) { |
| 827 | if (proc.pm2_env.pm_exec_path == full_path && |
| 828 | proc.pm2_env.name == app_conf.name) |
| 829 | managed_script = proc; |
| 830 | }); |
| 831 | |
| 832 | if (managed_script && |
| 833 | (managed_script.pm2_env.status == conf.STOPPED_STATUS || |
| 834 | managed_script.pm2_env.status == conf.STOPPING_STATUS || |
| 835 | managed_script.pm2_env.status == conf.ERRORED_STATUS)) { |
| 836 | // Restart process if stopped |
| 837 | var app_name = managed_script.pm2_env.name; |
| 838 | |
| 839 | that._operate('restartProcessId', app_name, opts, function(err, list) { |
| 840 | if (err) return cb ? cb(new Error(err)) : that.exitCli(conf.ERROR_EXIT); |
| 841 | Common.printOut(conf.PREFIX_MSG + 'Process successfully started'); |
| 842 | return cb(true, list); |
| 843 | }); |
| 844 | return false; |
| 845 | } |
| 846 | else if (managed_script && !opts.force) { |
| 847 | Common.err('Script already launched, add -f option to force re-execution'); |
| 848 | return cb(new Error('Script already launched')); |
| 849 | } |
| 850 | |
| 851 | var resolved_paths = null; |
| 852 | |
| 853 | try { |
| 854 | resolved_paths = Common.resolveAppAttributes({ |
| 855 | cwd : that.cwd, |
| 856 | pm2_home : that.pm2_home |
| 857 | }, app_conf); |
| 858 | } catch(e) { |
| 859 | Common.err(e.message); |
| 860 | return cb(Common.retErr(e)); |
| 861 | } |
| 862 | |
| 863 | Common.printOut(conf.PREFIX_MSG + 'Starting %s in %s (%d instance' + (resolved_paths.instances > 1 ? 's' : '') + ')', |
| 864 | resolved_paths.pm_exec_path, resolved_paths.exec_mode, resolved_paths.instances); |
| 865 | |
| 866 | if (!resolved_paths.env) resolved_paths.env = {}; |
| 867 | |
| 868 | // Set PM2 HOME in case of child process using PM2 API |
| 869 | resolved_paths.env['PM2_HOME'] = that.pm2_home; |
| 870 | |
| 871 | var additional_env = Modularizer.getAdditionalConf(resolved_paths.name); |
| 872 | Object.assign(resolved_paths.env, additional_env); |
| 873 | |
| 874 | // Is KM linked? |
| 875 | resolved_paths.km_link = that.gl_is_km_linked; |
| 876 |