显示 SysV 服务
chkconfig —list
systemd 服务
systemctl list-unit-files
查看在具体 target 启用的服务请执行
systemctl list-dependencies [target]
eg:systemctl list-dependencies aliyun.service

必备条件

  1. # 找到node位置
  2. $ whereis node
  3. # node: /usr/local/system_config/node-v16.13.1-linux-x64/bin/node
  4. # 建立软连接
  5. $ sudo ln -s /usr/local/system_config/node-v16.13.1-linux-x64/bin/node /usr/bin/node
  6. # 全局安装forever
  7. $ npm i forever -g
  8. # 找到forever位置
  9. $ whereis node
  10. /usr/local/system_config/node-v16.13.1-linux-x64/bin/forever
  11. # 软连接文件位置
  12. $ cd /usr/bin
  13. # 如果文件名被占用,就删掉之前的 rm -rf node

linux上npm install时提示/usr/bin/env: node: Permission denied

配置快捷命令

1.vim /etc/init.d/nodeforever 复制一下文本,粘贴进去

  1. #!/bin/sh
  2. # chkconfig: 2345 85 15
  3. # description: Startup script for nodeserver.
  4. # exit on first error
  5. set -e
  6. # points to the root for forever config
  7. export "FOREVER_ROOT=/root/.forever"
  8. #LOG=/data/nodeserver/log
  9. #PID=/data/nodeserver/pid
  10. starts=(
  11. "/usr/local/system_config/node-v16.13.1-linux-x64/bin/forever --sourceDir /usr/local/nginx/web-api start ./bin/www"
  12. )
  13. stops=(
  14. "/usr/local/system_config/node-v16.13.1-linux-x64/bin/forever stopall"
  15. )
  16. # start function
  17. do_start(){
  18. for i in "${starts[@]}"
  19. do
  20. $i
  21. done
  22. }
  23. # stop function
  24. do_stop(){
  25. for i in "${stops[@]}"
  26. do
  27. $i
  28. done
  29. }
  30. # Decide what command is being called
  31. case "$1" in
  32. start)
  33. echo "Starting nodeserver..."
  34. do_start
  35. echo "done."
  36. ;;
  37. stop)
  38. echo "Stoping nodeserver..."
  39. do_stop
  40. echo "done."
  41. ;;
  42. restart)
  43. echo "Restarting nodeserver..."
  44. do_stop
  45. do_start
  46. echo "done."
  47. ;;
  48. *)
  49. echo "Usage: nodeserver {start|stop|restart}" >&2
  50. exit 3
  51. ;;
  52. esac
  53. exit 0

2.chmod 755 /etc/init.d/nodeforever
3.chkconfig —add nodeforever
4.使用nodeserver命令
service nodeforever start
service nodeforever stop
service nodeforever restart
image.png

设置开机启动

cd /lib/systemd/system/ (主目录)
cd /usr/lib/systemd/system/
# vim node-forever.service

  1. [Unit]
  2. Description=node-forever.service
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/bin/forever /usr/local/nginx/web-api start ./bin/www
  7. ExecReload=/usr/bin/forever restartall
  8. ExecStop=/usr/bin/forever stopall
  9. PrivateTmp=true
  10. [Install]
  11. WantedBy=multi-user.target

systemctl enable node-forever.service
systemctl disable node-forever.service
systemctl list-unit-files | grep node 查看状态
systemctl is-enabled node-forever.service