[nodejs][ubuntu]—forever開機自動重啟服務

forever很方便,可是重開機就GG惹,要手動一筆一筆重啟

這裡整合 https://www.tzyu.tw/wordpress/2021/11/02/ubunut-%e8%87%aa%e5%8b%95%e9%96%8b%e6%a9%9f%e5%95%9f%e5%8b%95/

這一篇,繼續設定forever開機啟動

在 /etc/rc.local 裡面加上指令

文件裡面打上

#!/bin/bash

sudo forever cleanlogs

cd /usr/local/nodejs/test1/ && sudo forever start -w --minUptime=1000 --spinSleepTime=1000 --uid ABC app.js

cd /usr/local/nodejs/test2/ && sudo forever start -w --minUptime=1000 --spinSleepTime=1000 --uid test app.js

cleanlogs是必備的

Ubuntu 編輯排程

檢視

sudo crontab -l

編輯

sudo crontab -e

說明

# ┌───────────── 分鐘   (0 - 59)
# │ ┌─────────── 小時   (0 - 23)
# │ │ ┌───────── 日     (1 - 31)
# │ │ │ ┌─────── 月     (1 - 12)
# │ │ │ │ ┌───── 星期幾 (0 - 7,0 是週日,6 是週六,7 也是週日)
# │ │ │ │ │
# * * * * * /path/to/command

範例

# 每天早上 8 點 30 分執行
30 08 * * * aaa.sh --your --parameter

# 每週日下午 6 點 30 分執行
30 18 * * 0 aaa.sh --your --parameter

# 每週日下午 6 點 30 分執行
30 18 * * Sun aaa.sh --your --parameter

# 每年 6 月 10 日早上 8 點 30 分執行
30 08 10 06 * aaa.sh --your --parameter

# 每月 1 日、15 日、29 日晚上 9 點 30 分各執行一次
30 21 1,15,29 * * aaa.sh --your --parameter

# 每隔 10 分鐘執行一次
*/10 * * * * aaa.sh --your --parameter

# 從早上 9 點到下午 6 點,凡遇到整點就執行
00 09-18 * * * aaa.sh --your --parameter

參考來源

ubuntu 自動開機啟動
sudo vim /lib/systemd/system/rc-local.service

請將下列三行添加至最尾處

[Install] 
WantedBy=multi-user.target
Alias=rc-local.service

存檔後

sudo vim /etc/rc.local
#在這添加要開機自動執行的腳本或命令

可執行權限

sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo service rc-local start && sudo service rc-local status

如果顯示失敗,多半是啟動文件錯誤,啟動文件前面要加上

#!/bin/sh -e
# @file /etc/rc.local

後面要加上

exit 0

重開測試

sudo reboot

Ubuntu 加入Google驗證(兩段式驗證)

1.安裝google 驗證器

sudo apt-get install libpam-google-authenticator

2.接下來視窗放到最大,準備掃描視窗的QrCode,

google-authenticator

掃描視窗的QrCode,輸入手機驗證碼,接下來一律按Y,如果無法掃描,可以手動輸入螢幕上驗證碼手動加入

2.1文字模式登入(如果3有設定就不用2.1)

sudo vim /etc/pam.d/sshd
#貼上  auth required pam_google_authenticator.so

2.2 sudo vim /etc/ssh/sshd_config

# 原本有可能是 KbdInteractiveAuthentication no
# 調整成 
ChallengeResponseAuthentication yes 

sudo service ssh restart

3.桌面模式登入

sudo vim /etc/pam.d/common-auth
#最後一行,貼上
auth required pam_google_authenticator.so nullok

收工