I've noticed that apps started with PM2 will keep running even after processes have been stopped or deleted. This doesn't always happen, especially if the process has just been started, but if it's been left to run for a while it will always happen.
I start my process with:
pm2 start npm --name "next" -- start
This works as intended.
However if I come back to it a couple of days later and run:
pm2 stop next
The process will be stopped and disappear from the output of pm2 ls
.
Unfortunately the app itself is still running in the background and the only way I have found to stop it is completely rebooting my server which isn't optimal.
My problem is similar to the one mentioned here, however the solution didn't work for me.
Output of ps aux || grep node
:
ps aux || grep "node"
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.5 225444 5868 ? Ss Sep09 1:42 /lib/systemd/systemd --system --deserialize 38
root 2 0.0 0.0 0 0 ? S Sep09 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< Sep09 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? I< Sep09 0:00 [mm_percpu_wq]
root 7 0.0 0.0 0 0 ? S Sep09 0:33 [ksoftirqd/0]
root 8 0.0 0.0 0 0 ? I Sep09 2:47 [rcu_sched]
root 9 0.0 0.0 0 0 ? I Sep09 0:00 [rcu_bh]
root 10 0.0 0.0 0 0 ? S Sep09 0:00 [migration/0]
root 11 0.0 0.0 0 0 ? S Sep09 0:10 [watchdog/0]
root 12 0.0 0.0 0 0 ? S Sep09 0:00 [cpuhp/0]
root 13 0.0 0.0 0 0 ? S Sep09 0:00 [kdevtmpfs]
root 14 0.0 0.0 0 0 ? I< Sep09 0:00 [netns]
root 15 0.0 0.0 0 0 ? S Sep09 0:00 [rcu_tasks_kthre]
root 16 0.0 0.0 0 0 ? S Sep09 0:00 [kauditd]
root 17 0.0 0.0 0 0 ? S Sep09 0:01 [khungtaskd]
root 18 0.0 0.0 0 0 ? S Sep09 0:00 [oom_reaper]
root 19 0.0 0.0 0 0 ? I< Sep09 0:00 [writeback]
root 20 0.0 0.0 0 0 ? S Sep09 0:00 [kcompactd0]
root 21 0.0 0.0 0 0 ? SN Sep09 0:00 [ksmd]
root 22 0.0 0.0 0 0 ? SN Sep09 0:00 [khugepaged]
root 23 0.0 0.0 0 0 ? I< Sep09 0:00 [crypto]
root 24 0.0 0.0 0 0 ? I< Sep09 0:00 [kintegrityd]
root 25 0.0 0.0 0 0 ? I< Sep09 0:00 [kblockd]
root 26 0.0 0.0 0 0 ? I< Sep09 0:00 [ata_sff]
root 27 0.0 0.0 0 0 ? I< Sep09 0:00 [md]
root 28 0.0 0.0 0 0 ? I< Sep09 0:00 [edac-poller]
root 29 0.0 0.0 0 0 ? I< Sep09 0:00 [devfreq_wq]
root 30 0.0 0.0 0 0 ? I< Sep09 0:00 [watchdogd]
root 34 0.0 0.0 0 0 ? S Sep09 21:32 [kswapd0]
root 35 0.0 0.0 0 0 ? I< Sep09 0:00 [kworker/u3:0]
root 36 0.0 0.0 0 0 ? S Sep09 0:00 [ecryptfs-kthrea]
root 78 0.0 0.0 0 0 ? I< Sep09 0:00 [kthrotld]
root 79 0.0 0.0 0 0 ? I< Sep09 0:00 [acpi_thermal_pm]
root 80 0.0 0.0 0 0 ? S Sep09 0:00 [scsi_eh_0]
root 81 0.0 0.0 0 0 ? I< Sep09 0:00 [scsi_tmf_0]
root 82 0.0 0.0 0 0 ? S Sep09 0:00 [scsi_eh_1]
root 83 0.0 0.0 0 0 ? I< Sep09 0:00 [scsi_tmf_1]
root 89 0.0 0.0 0 0 ? I< Sep09 0:00 [ipv6_addrconf]
root 98 0.0 0.0 0 0 ? I< Sep09 0:00 [kstrp]
root 115 0.0 0.0 0 0 ? I< Sep09 0:00 [charger_manager]
root 153 0.0 0.0 0 0 ? S Sep09 0:00 [scsi_eh_2]
root 154 0.0 0.0 0 0 ? I< Sep09 0:00 [scsi_tmf_2]
root 155 0.0 0.0 0 0 ? I< Sep09 0:58 [kworker/0:1H]
root 266 0.0 0.0 0 0 ? I< Sep09 0:00 [raid5wq]
root 318 0.0 0.0 0 0 ? S Sep09 0:54 [jbd2/vda1-8]
root 319 0.0 0.0 0 0 ? I< Sep09 0:00 [ext4-rsv-conver]
root 404 0.0 0.0 0 0 ? I< Sep09 0:00 [iscsi_eh]
root 408 0.0 0.0 105904 228 ? Ss Sep09 0:00 /sbin/lvmetad -f
root 413 0.0 0.0 0 0 ? I< Sep09 0:00 [ib-comp-wq]
root 414 0.0 0.0 0 0 ? I< Sep09 0:00 [ib_mcast]
root 415 0.0 0.0 0 0 ? I< Sep09 0:00 [ib_nl_sa_wq]
root 417 0.0 0.0 0 0 ? I< Sep09 0:00 [rdma_cm]
root 698 0.0 0.9 501844 9800 ? Ss Sep18 1:46 /usr/sbin/apache2 -k start
root 842 0.0 0.1 652392 1420 ? Ssl Sep09 0:18 /usr/bin/lxcfs /var/lib/lxcfs/
root 843 0.0 0.4 71488 4416 ? Ss Sep09 0:05 /lib/systemd/systemd-logind
root 848 0.0 0.7 187700 7924 ? Ssl Sep09 0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
root 854 0.0 0.1 287992 1416 ? Ssl Sep09 2:48 /usr/lib/accountsservice/accounts-daemon
root 856 0.0 0.1 31748 1240 ? Ss Sep09 0:07 /usr/sbin/cron -f
syslog 861 0.0 0.1 263036 1776 ? Ssl Sep09 1:51 /usr/sbin/rsyslogd -n
root 864 0.0 0.7 170876 7808 ? Ssl Sep09 0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
message+ 865 0.0 0.3 50280 3092 ? Ss Sep09 2:10 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
daemon 891 0.0 0.0 28332 212 ? Ss Sep09 0:00 /usr/sbin/atd -f
root 895 0.0 0.2 72296 2068 ? Ss Sep09 2:17 /usr/sbin/sshd -D
root 903 0.0 0.0 16412 140 ttyS0 Ss+ Sep09 0:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
root 907 0.0 0.1 288880 1524 ? Ssl Sep09 1:11 /usr/lib/policykit-1/polkitd --no-debug
root 908 0.0 0.0 14888 136 tty1 Ss+ Sep09 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
mysql 943 0.1 20.6 1161772 208636 ? Sl Sep09 68:26 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
leonardo 1068 0.0 0.1 76636 1028 ? Ss Sep09 0:00 /lib/systemd/systemd --user
leonardo 1072 0.0 0.2 111788 2388 ? S Sep09 0:00 (sd-pam)
root 6750 0.0 0.0 0 0 ? I Oct12 0:01 [kworker/0:1]
root 13084 0.0 0.0 42904 820 ? Ss Sep12 0:30 /lib/systemd/systemd-udevd
leonardo 16084 0.0 0.0 4632 72 ? S 11:26 0:00 sh -c NODE_PATH=. NODE_ENV=production node server.js
leonardo 16085 0.0 9.0 1303708 90912 ? Sl 11:26 0:05 node server.js
www-data 16095 0.0 6.5 592644 65988 ? S 11:26 0:02 /usr/sbin/apache2 -k start
www-data 16096 0.0 6.1 512560 62084 ? S 11:26 0:01 /usr/sbin/apache2 -k start
root 16993 0.0 0.0 105692 996 ? Ss 12:39 0:00 sshd: leonardo [priv]
leonardo 17093 0.0 0.0 107992 1008 ? S 12:39 0:00 sshd: leonardo@pts/1
leonardo 17094 0.0 0.1 23204 1716 pts/1 Ss+ 12:39 0:00 -bash
systemd+ 17556 0.0 0.0 71848 724 ? Ss Sep12 0:03 /lib/systemd/systemd-networkd
systemd+ 17571 0.0 0.1 70756 1872 ? Ss Sep12 1:13 /lib/systemd/systemd-resolved
systemd+ 17585 0.0 0.0 141928 596 ? Ssl Sep12 0:04 /lib/systemd/systemd-timesyncd
root 17604 0.0 4.3 184004 43936 ? S<s Sep12 7:58 /lib/systemd/systemd-journald
root 18184 0.0 0.0 0 0 ? I 14:11 0:00 [kworker/0:0]
leonardo 18215 0.0 4.4 966088 44652 ? Ssl 14:12 0:01 PM2 v3.5.1: God Daemon (/home/leonardo/.pm2)
www-data 18227 0.0 5.7 590632 58468 ? S 14:12 0:00 /usr/sbin/apache2 -k start
www-data 18231 0.0 4.6 511832 46832 ? S 14:12 0:00 /usr/sbin/apache2 -k start
www-data 18232 0.0 2.0 504524 20264 ? S 14:12 0:00 /usr/sbin/apache2 -k start
root 18261 0.0 0.0 0 0 ? I 14:14 0:00 [kworker/u2:1]
root 18311 0.0 0.0 0 0 ? I 14:19 0:00 [kworker/u2:0]
www-data 18368 0.0 3.6 505148 37244 ? S 14:26 0:00 /usr/sbin/apache2 -k start
root 18438 0.0 0.0 0 0 ? I 14:35 0:00 [kworker/u2:2]
www-data 18441 0.0 1.9 504244 19308 ? S 14:35 0:00 /usr/sbin/apache2 -k start
www-data 18442 0.0 1.9 504244 19308 ? S 14:35 0:00 /usr/sbin/apache2 -k start
www-data 18443 0.0 1.9 504244 19308 ? S 14:35 0:00 /usr/sbin/apache2 -k start
www-data 18445 0.0 1.9 504304 19440 ? S 14:35 0:00 /usr/sbin/apache2 -k start
root 18458 0.0 0.7 105684 7216 ? Ss 14:36 0:00 sshd: leonardo [priv]
leonardo 18538 0.0 0.5 107984 5504 ? S 14:36 0:00 sshd: leonardo@pts/0
leonardo 18539 0.1 0.5 23204 5144 pts/0 Ss 14:36 0:00 -bash
leonardo 18558 0.0 0.3 40092 3460 pts/0 R+ 14:37 0:00 ps aux
root 24135 0.0 0.0 0 0 ? I< Sep12 0:00 [xfsalloc]
root 24136 0.0 0.0 0 0 ? I< Sep12 0:00 [xfs_mru_cache]
do-agent 28788 0.0 1.3 496372 14064 ? Ssl Sep14 5:51 /opt/digitalocean/bin/do-agent --syslog
My server.js
:
const express = require( 'express' )
const axios = require( 'axios' )
const next = require( 'next' )
const cacheableResponse = require('cacheable-response')
// Import middleware.
const routes = require( './routes' )
// Setup app.
const app = next( { dev: 'production' !== process.env.NODE_ENV } )
const handle = app.getRequestHandler()
const handler = routes.getRequestHandler( app )
const ssrCache = cacheableResponse({
ttl: 1000 * 60 * 60, // 1hour
get: async ({ req, res, pagePath, queryParams }) => ({
data: await app.renderToHTML(req, res, pagePath, queryParams)
}),
send: ({ data, res }) => res.send(data)
})
app.prepare()
.then( () => {
// Create server.
const server = express();
// Use our handler for requests.
server.use( handler );
// Don't remove. Important for the server to work. Default route.
server.get( '*', ( req, res ) => {
ssrCache({ req, res })
} );
// Get current port.
const port = process.env.PORT || 8080;
// Error check.
server.listen( port, err => {
if ( err ) {
throw err;
}
// Where we starting, yo!
console.log( `> Ready on port ${port}...` );
} );
} );
This can happen due to multiple reason. Please check if the app itself is running any demon from within. What do you see when you run ps aux || grep node
. If you can post your app.js
or server.js
or index.js
or anything which acts as main file of your application file here that would help to diagnose the issue.
if you are setting any environment variable like production you can do it explicitly i.e export NODE_ENV=production
You can start the server.js app without npm with pm2 with below command
pm2 start server.js -- --prod
This should work without any problem.
For anything more than this i would recommend the pm2 config file this is the right way of using pm2 scripts.