显示 SysV 服务
chkconfig —list
systemd 服务
systemctl list-unit-files
查看在具体 target 启用的服务请执行
systemctl list-dependencies [target]
eg:systemctl list-dependencies aliyun.service
必备条件
# 找到node位置
$ whereis node
# node: /usr/local/system_config/node-v16.13.1-linux-x64/bin/node
# 建立软连接
$ sudo ln -s /usr/local/system_config/node-v16.13.1-linux-x64/bin/node /usr/bin/node
# 全局安装forever
$ npm i forever -g
# 找到forever位置
$ whereis node
/usr/local/system_config/node-v16.13.1-linux-x64/bin/forever
# 软连接文件位置
$ cd /usr/bin
# 如果文件名被占用,就删掉之前的 rm -rf node
linux上npm install时提示/usr/bin/env: node: Permission denied
配置快捷命令
1.vim /etc/init.d/nodeforever 复制一下文本,粘贴进去
#!/bin/sh
# chkconfig: 2345 85 15
# description: Startup script for nodeserver.
# exit on first error
set -e
# points to the root for forever config
export "FOREVER_ROOT=/root/.forever"
#LOG=/data/nodeserver/log
#PID=/data/nodeserver/pid
starts=(
"/usr/local/system_config/node-v16.13.1-linux-x64/bin/forever --sourceDir /usr/local/nginx/web-api start ./bin/www"
)
stops=(
"/usr/local/system_config/node-v16.13.1-linux-x64/bin/forever stopall"
)
# start function
do_start(){
for i in "${starts[@]}"
do
$i
done
}
# stop function
do_stop(){
for i in "${stops[@]}"
do
$i
done
}
# Decide what command is being called
case "$1" in
start)
echo "Starting nodeserver..."
do_start
echo "done."
;;
stop)
echo "Stoping nodeserver..."
do_stop
echo "done."
;;
restart)
echo "Restarting nodeserver..."
do_stop
do_start
echo "done."
;;
*)
echo "Usage: nodeserver {start|stop|restart}" >&2
exit 3
;;
esac
exit 0
2.chmod 755 /etc/init.d/nodeforever
3.chkconfig —add nodeforever
4.使用nodeserver命令
service nodeforever start
service nodeforever stop
service nodeforever restart
设置开机启动
cd /lib/systemd/system/ (主目录)
cd /usr/lib/systemd/system/
# vim node-forever.service
[Unit]
Description=node-forever.service
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/forever /usr/local/nginx/web-api start ./bin/www
ExecReload=/usr/bin/forever restartall
ExecStop=/usr/bin/forever stopall
PrivateTmp=true
[Install]
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