How can I create an app that can restart itself? I want to create an app that sets up a web-admin which can restart itself. Is this possible? If so, how? I was thinking this might be possible with the process
global variable that is built into node.

- 2,575
- 1
- 28
- 36

- 7,953
- 19
- 62
- 119
-
Several modules from https://github.com/joyent/node/wiki/modules#wiki-build-and-deployment claim to make that easy. – sarnold Feb 20 '12 at 07:53
8 Answers
LK"I
It is possible without external dependencies:
console.log("This is pid " + process.pid);
setTimeout(function () {
process.on("exit", function () {
require("child_process").spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached : true,
stdio: "inherit"
});
});
process.exit();
}, 5000);
source : https://gist.github.com/silverwind/d0802f7a919ae86ff25e

- 1,520
- 1
- 14
- 11
-
Yes, it works well on my local, but it doens't work on docker container. It would be great if you provide me some advice. – Zhong Ri Jun 18 '19 at 12:32
-
It wors, but . . . how to stop it? In other words, if I want to stop the process as I need to change something in the script, how can I do it? It looks like when restarts, ignore the changes done in the existing scripts! – Power Engineering May 04 '22 at 15:46
I have run Forever several times and it is easy to get started with. Check it out at: https://github.com/nodejitsu/forever

- 17,816
- 1
- 22
- 21
-
@Lord: pay special attention to the `restart` and `restartall` commands. – sarnold Feb 20 '12 at 22:55
-
huh, those weren't in the readme. I'm trying to get it installed right now, but I'm having some issues on windows. http://stackoverflow.com/questions/9366916/cant-install-forever-on-windows – LordZardeck Feb 20 '12 at 22:57
I know it's a little late to reply however I had a similar requirement. I wanted to restart my node process whenever I made a configuration change. I'm using pm2 to manage my node processes so it turned out to be really easy.
After making a configuration change, i execute process.exit() from within the node process. As far as I can see, the process exits then pm2 restarts the process fine.
Not sure yet if there are any side effects but it seems to be working fine for me right now.

- 181
- 1
- 5
you can run your app using child process and manipulate it how needed: https://nodejs.org/api/child_process.html
use forever, pm2 or whatever thing to restart after death and kill itself with process.exit() https://nodejs.org/api/process.html

- 24,652
- 10
- 111
- 109
Not from the process itself but it might be useful to people that land on here.
I add it to systemd of Linux and it starts up on network.target
logs the console to /var/logs/<app>.log
and it restarts on failure. So you can just force a process.exit
on it.
sudo touch /lib/systemd/system/<app>.service
sudo tee -a /lib/systemd/system/<app>.service > /dev/null <<EOT
[Unit]
Description=<app>
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=/bin/node --prefix /home/$USER/path/to/app
Restart=on-failure # or always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<app>
[Install]
WantedBy=multi-user.target
EOT
sudo touch /etc/rsyslog.d/<app>.conf
sudo tee -a /etc/rsyslog.d/<app>.conf > /dev/null <<EOT
if $programname == '<app>' then /var/log/<app>.log
& stop
EOT
sudo systemctl enable <app>
sudo systemctl daemon-reload
sudo systemctl restart rsyslog
sudo systemctl start <app>

- 189
- 8
i know the question is a little old but it may help someone later : i would suggest to use nodeJS Cluster & Worker for this purpose!
const cluster = require('cluster');
if (cluster.isMaster ?? cluster.isPrimary) {
cluster.fork();
cluster.on("exit", (worker, code) => {
console.log(`[master] worker #${worker.id} down, restarting\n`);
cluster.fork();
});
process.on("SIGINT", () => { });
} else {
console.log(`\nnew worker #${cluster.worker.id}\n`);
process.on("SIGINT", () => {
console.log(`[worker#${cluster.worker.id}] SIGINT received! dying gracefully!!\n`);
process.exit(0);
});
}
try running it with nodejs and hitting the ctrl+c combination.
it will just restart with the new code running.
you can kill the master with sending any signal other than SIGINT

- 740
- 7
- 16
create file nodemon.sh
#!/usr/bin/env bash
while true; do
sleep 20
echo "// nodemon $(date)" >config.js
done
permition sudo chmod +x nodemon.sh
run it
./nodemon.sh

- 1,747
- 15
- 9
Yes, upstart will restart your process without a nodemon
.
npm install -g nodemon
sudo nodemon server.js
nodemon
will watch the files in the directory that nodemon
was started, and if they change, it will automatically restart your node application.
-
I don't think the OP wants their service to keep restarting forever. Rather, they seem to want to trigger a restart under specific conditions, or exit if those conditions are not met. – aggregate1166877 Feb 03 '22 at 21:55