SoFunction
Updated on 2025-03-06

Use forever to implement project self-start

So can we use forever plus startup scripts to solve the above problems? The answer is of course yes, but it's a bit troublesome, and the official forever lacks detailed configuration documents. I also took some detours when configuring, so I will go into details below.

Note: The experimental environment of this article is Ubuntu Server 12.04 LTS x86_64, and the configuration on CentOS is simpler.

At first, I thought of trying to add a forever start xxx to /etc/, but I found that Ubuntu (the same as other systems) is not bad for me. The main contradiction is that mongodb can run by using this method, but forever cannot. In desperation, I should consider it from the perspective of /etc/.

The premise is to make forever first. The method is very simple. Just execute the following command:

npm install forever -g

After the installation is complete, use a simple Node program to test it:

forever start 
forever stop 
forever restart 

As long as the error is not prompted, it means that forever is usable, that is, the basic conditions for using forever to open a Node project in the background are already met, and the rest is to write a startup script.

The basic content of the script is as follows, thanks to the original author for his hard work:

#!/bin/bash
### BEGIN INIT INFO
# Provides:  xiyoulib
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop:  0 1 6
# Short-Description: Start daemon at boot time
# Description:  Enable service provided by daemon.
### END INIT INFO
# chkconfig: 345 88 08
# description: Forever for 

DEAMON=//XiyouLibNodeExpress/bin/www  #The startup script file of your own Node project needs to be filled in hereLOG=//log/log #Optional, log file directoryPID=//pid #Required content to record the process number of forever
export PATH=$PATH:/usr/local/bin #Specify the Node executable program installation directory here, mine is /usr/local/binexport NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules #Here is the path to the Node class library
#The contents below are not required to be modified
node=node
forever=forever

case "$1" in
 start)
  $forever start -l $LOG --pidFile $PID -a $DEAMON
  ;;
 stop)
  $forever stop --pidFile $PID $DEAMON
  ;;
 stopall)
  $forever stopall --pidFile $PID
  ;;
 restartall)
  $forever restartall --pidFile $PID
  ;;
 reload|restart)
  $forever restart -l $LOG --pidFile $PID -a $DEAMON
  ;;
 list)
  $forever list
  ;;
 *)
  echo "Usage: //node {start|stop|restart|reload|stopall|restartall|list}"
  exit 1
  ;;
esac

Here is a reminder: it is best to create a directory in the root directory for the Node project, such as /, and then set the permission to 754, which can avoid the trouble caused by some permission problems!

Since it is using the Ubuntu Server system, MongoDB startup service is also configured, and the following statements are added to its script:

# Required-Start:  $all
# Required-Stop:   $all

So when I add it myself in the future, the system will prompt an error. So in the startup script of the Node project, I added the previous string of explanation comments for the Ubuntu Server system to set it up. If it is on CentOS, similar problems should not occur. Pay special attention to this!

That is, the following explanation information:

### BEGIN INIT INFO
# Provides:     xiyoulib
# Required-Start:  $all
# Required-Stop:   $all
# Default-Start:   2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Start daemon at boot time
# Description:    Enable service provided by daemon.
### END INIT INFO

After the script is edited, use the chkconfig --list directive to check whether the service you added is effective, that is, all 3 and 5 must be on to achieve self-start on.

If 3 and 5 are not set to on, please execute chkconfig --level 35 [your service name] on. Ubuntu Server may report some warnings, but as long as the 3 and 5 of the required service can be changed to on, other errors can be ignored (I feel that this is the system's own business).

After the setup is completed, the Node project can be started on Linux. You can shutdown -r now try to see if it can be started. After starting, you can directly access the port number and virtual directory you set. If you want it, the task will be done!

But if it is wrong, check some scripts carefully and make relevant modifications based on the error. After all, I also tried it!