I have created API using node js and my API is live now (in production) but now I always want to open Cpanel and terminal and run command to start node js API. but now I want like I just run one-time command in terminal and it will run automatically even in my computer is shutdown. or there is another way to do it. and I google it I found some code but it’s not work
first, I try this
#!/bin/bash
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running." >/dev/null 2>&1
else
echo "Process is not running."
PATH=$PATH:/usr/local/bin
pm2 start /path/to/your/node/application
fi
then i create bat file. and just simple add node app.js
then i create php file and
use this code shell_exec('npm start');
and shell_exec('sh script.sh');
but nothing work.
2
Answers
If you’re using pm2 to manage the node process, you can configure pm2 to auto restart the node process after you’re computer restarts. refer below link to configure pm2
https://pm2.keymetrics.io/docs/usage/startup/
Hello I Find This answer you just have install pm2 and run
pm2 startup
then runpm2 save
to save your query.for more information https://pm2.keymetrics.io/docs/usage/startup/