【Linux】创建后台服务器

Systemctl

创建文件/etc/systemd/system/qsign.service,注意Service的参数需要使用绝对路径

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=unidbg-fetch-qsign
After=network.target

[Service]
Environment="JAVA_HOME=/usr/local/btjdk/jdk-11.0.19"WorkingDirectory=/www/wwwroot/unidbg-fetch-qsign
ExecStart=bash bin/unidbg-fetch-qsign --basePath=txlib/8.9.70
Restart=always
RestartSec=60s

[Install]
WantedBy=multi-user.target

命令

sudo systemctl daemon-reload 重载服务,每次修改都要
netstat -lntp 查看端口情况
sudo systemctl start qsign 启动服务
sudo systemctl stop qsign 停止
sudo systemctl restart qsign 重启
sudo systemctl enable qsign 设置开机启动
sudo systemctl disable qsign 关闭开机启动
sudo systemctl status qsign 查看状态
sudo journalctl -u qsign 显示服务日志
sudo journalctl -f 持续输出所有后台服务器日志

Screen

如果screen内部运行的程序需要root权限,则也需要使用sudo操作下面的命令

screen -S wardswar-fantasy 创建会话
screen -r {名称/id} 重连会话
screen -ls 查看当前所有的会话
screen -X -S {名称/id} quit 关闭会话
screen -dmS wardswar-fantasy bash -c “dotnet Main.dll -m Develop” 直接创建一个在后台跑的任务

在会话内部快捷键

Ctrl+A,然后按D 退出会话
Ctrl + A,然后按 C 创建新窗口(可在一个会话中开多个”标签页”)
Ctrl + A,然后按 N / P 切换到下一个(N) / 上一个(P) 窗口
Ctrl + A,然后按 " 列出所有窗口(双引号键)
Ctrl + A,然后按 A 重命名当前窗口(大写字母A)

tmux

安装

1
sudo apt install tmux
tmux new -s wordswar-fantasy 创建
tmux attach -t wordswar-fantasy 进入
tmux ls 查看
tmux kill-session wordswar-fantasy 杀死

tmux attach可简写成tmux a,不指定-t时默认连接到最近使用的会话(如果有多个会话,会交互选择)

会话内快捷键

Ctrl+B,然后D 分离
Ctrl+B,然后C 新建窗口
Ctrl+B,然后N 下一个窗口
Ctrl+B,然后P 上一个窗口
Ctrl+B,然后W 窗口列表
Ctrl+B,然后, 重命名窗口

Pane(分屏)

Ctrl+B,然后% 左右分屏
Ctrl+B,然后" 上下分屏
Ctrl+B,然后方向键 在pane之间切换
exit 关闭pane

配置文件~/.tmux.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 开启鼠标支持
# 可以:
# - 鼠标点击切换 pane
# - 鼠标选择 window
# - 鼠标滚轮滚动历史
# - 拖拽调整 pane 大小
set -g mouse on


# 设置终端历史记录上限
# 默认 tmux 历史很少,长日志会被冲掉
# 这里改成 100000 行
set -g history-limit 100000


# 复制模式使用 vim 风格快捷键
# 例如:
# - hjkl 移动
# - / 搜索
# - v 开始选择
# - y 复制
setw -g mode-keys vi


# window 编号从 1 开始
# 默认是:
# 0 1 2 3
# 改后:
# 1 2 3 4
set -g base-index 1


# pane 编号从 1 开始
# 默认 pane 是 0 开始
setw -g pane-base-index 1


# 绑定快捷键:
# Ctrl+B 然后按 r
# 作用:
# 重新加载 ~/.tmux.conf 配置文件
# 并显示:
# Config reloaded!
bind r source-file ~/.tmux.conf \; display "Config reloaded!"


# 使用 Alt + 方向键直接切 pane
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D


# 重新分割 pane 后自动保持当前路径
bind '"' split-window -v -c '#{pane_current_path}'
bind % split-window -h -c '#{pane_current_path}'


# 提高颜色支持
set -g default-terminal "screen-256color"


# 更快响应 Esc
# vim 下体验更好
set -sg escape-time 0


# 自动重命名窗口
# 例如窗口会变成:
# vim / node / htop
set-option -g automatic-rename on

pm2

安装

1
2
3
4
5
// 安装npm
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
// 安装pm2
sudo npm install -g pm2

启动

1
2
3
pm2 start dotnet --name wordswardev --cwd /home/wordswardev/wordswar-fantasy-dev/app/server/linux-x64 -- Main.dll -m Develop

pm2 start dotnet --name wordswardy --cwd /home/ubuntu/wordswar-fantasy-dy/app/server/linux-x64 -- Main.dll -m Release --pid 1

设置开机自启

1
2
3
4
5
6
7
8
9
10
// 先保存一遍
pm2 save
// 获取命令
pm2 startup
// 会输出类似
// sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u ubuntu --hp /home/ubuntu
// 把输出的命令复制执行

// 再保存一遍
pm2 save
pm2 list 列出所有项目
pm2 show {name} 查看详情
pm2 delete {name} 删除
pm2 logs {name} 查看log