linux安装golang

1
2
3
4
5
6
7
8
uname -a 查看系统信息
uname -m 查看系统架构
Curl下载指定版本的golang
Curl -o localname url
Curl -O url 以远程文件名保存到本地
tar -xzvf xxx.tar.gz 解压
移动到/usr/local/bin
软连接ln -s /usr/loacl/bin/go/bin/go /usr/bin/xxx

linux解压

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
linux tar命令 压缩、打包、解压 详解

1、常用压缩命令

tar –czvf 压缩后的文件.tar.gz 要压缩的文件

2、常用解压命令

tar –xzvf 解压后的文件.tar.gz 【要解压的目录】

3、参数意义

-c: 建立压缩档案

-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。

-j:有bz2属性的

-O:将文件解开到标准输出

-r:向压缩归档文件末尾追加文件

-t:查看内容

-u:更新原压缩包中的文件

-v:显示所有过程

-x:解压

-z:有gzip属性的

-Z:有compress属性的

4、总结

*.tar 用 tar –xvf 解压

*.gz 用 gzip -d或者gunzip 解压

*.tar.gz和*.tgz 用 tar –xzf 解压

*.bz2 用 bzip2 -d或者用bunzip2 解压

*.tar.bz2用tar –xjf 解压

*.Z 用 uncompress 解压

*.tar.Z 用tar –xZf 解压

*.rar 用 unrar e解压

*.zip 用 unzip 解压

python_selenium

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
from openpyxl.reader.excel import load_workbook
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

# 安装driver,压缩包,将其解压,复制到Python的与Scripts目录同级文件夹下
# 创建一个新的工作簿
wb = load_workbook('output.xlsx')
ws = wb.active # 获取活动工作表

for page_num in range(84,154):
# option = selenium.webdriver.chrome.options.Options()
# option.binary_location = r"C:\Users\chrome\chrome-headless-shell.exe"
# driver = webdriver.Chrome(options=option)
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(f"https://www.baidu.com/{page_num}")
articles = driver.find_elements(By.XPATH,"/html/body/article")
# 列数据处理
for index,article in enumerate(articles[:2] + articles[3:]):
title = article.find_element(By.XPATH,"header/h2/a").text
try:
like_num = article.find_element(By.XPATH, "div/a[1]").text.split("(")[1].split(")")[0]
read_num = article.find_element(By.XPATH,"div/span[2]").text.split("(")[1].split(")")[0]
print(10*(page_num-1)+index+1)
print(title)
print(read_num)
print("*"*6)
ws.cell(row=10*(page_num-1)+index+1, column=1, value=title) # 第一列数据
ws.cell(row=10*(page_num-1)+index+1, column=2, value=read_num) # 第二列数据
ws.cell(row=10*(page_num-1)+index+1, column=3, value=like_num) # 第二列数据
# 保存 Excel 文件
wb.save('output.xlsx')
except:
pass
# 关闭浏览器
driver.quit()

vsc部分设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   // terminal encoding
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": ["/K","chcp 65001"],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
}
},
// file encoding
"files.autoGuessEncoding": true,
// gopls server
"go.useLanguageServer": true

golang micro service

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
service

package main

import (
"context"
"log"

pb "github.com/go-micro/examples/helloworld/proto"
"go-micro.dev/v4"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
rsp.Greeting = "Hello " + req.Name
return nil
}

func main() {
service := micro.NewService(
micro.Name("helloworld"),
micro.Address(":8080"),
)

service.Init()

pb.RegisterGreeterHandler(service.Server(), new(Greeter))

if err := service.Run(); err != nil {
log.Fatal(err)
}
}
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
client

package main

import (
"context"
"fmt"
example "github.com/go-micro/examples/helloworld/proto"
"go-micro.dev/v4"
"go-micro.dev/v4/client"
"go-micro.dev/v4/metadata"
)

func main() {
service := micro.NewService()
service.Init()

fmt.Println("\n--- Call example ---")
for i := 0; i < 10; i++ {
call(i, service.Client())
}
}

func call(i int, c client.Client) {
// Create new request to service go.micro.srv.example, method Example.Call
req := c.NewRequest("helloworld", "Greeter.Hello", &example.Request{
Name: "John",
})

// create context with metadata
ctx := metadata.NewContext(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})

rsp := &example.Response{}

// Call service
if err := c.Call(ctx, req, rsp); err != nil {
fmt.Println("call err: ", err, rsp)
return
}

fmt.Println("Call:", i, "rsp:", rsp.Greeting)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
proto

syntax = "proto3";

service Greeter {
rpc Hello(Request) returns (Response) {}
}

message Request {
string name = 1;
}

message Response {
string greeting = 2;
}

linux开机自启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
screen -dm 后台新建一个screen并执行命令bach -c "xxxx"


#!/bin/bash

# 会话名称
SESSION_NAME="bot"

# 检查是否已经存在该会话
if screen -list | grep -q "$SESSION_NAME"; then
echo "Screen session '$SESSION_NAME' already exists. Attaching..."
screen -r "$SESSION_NAME"
else
echo "Starting new screen session: $SESSION_NAME"
screen -S "$SESSION_NAME" -dm bash -c "cd /opt/Miao-Yunzai && npm run app; exec bash"
echo "NPM dev started in new screen session."
fi
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
#使用nano新建一个文件,Ctrl+o保存,Ctrl+x退出
sudo nano /etc/systemd/system/npm-dev.service
#确保你的脚本有执行权限
chmod +x /path/to/your/run_npm_dev.sh
#重启systemctl
sudo systemctl daemon-reload
#启动服务
sudo systemctl start npm-dev.service
#启用服务
sudo systemctl enable npm-dev.service
#查看脚本状态
sudo systemctl status npm-dev.service
#WorkingDirectory 执行命令的目录

[Unit]
Description=NPM Dev Screen Service
After=network.target

[Service]
ExecStart=/path/to/your/run_npm_dev.sh
WorkingDirectory=/path/to/your/project
StandardOutput=inherit
StandardError=inherit
Restart=always
User=your-username

[Install]
WantedBy=multi-user.target
1
2
3
支持rc.local的系统,可以在里面添加要运行的脚本
/etc/rc.local
bash /path/to/startup.sh

python单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1. 使用类的__new__方法
__new__ 是类的构造方法,负责创建实例。你可以通过重写这个方法来确保一个类只能有一个实例。

class Singleton:
_instance = None # 用于存储类的唯一实例

def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance

# 测试
singleton1 = Singleton()
singleton2 = Singleton()

print(singleton1 is singleton2) # 输出 True,表明两个对象是同一个实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2. 使用装饰器实现单例
装饰器可以将单例逻辑封装起来,使得实现单例的方式更加灵活和简洁。

def singleton(cls):
instances = {}

def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]

return get_instance

@singleton
class Singleton:
def __init__(self):
self.value = 42

# 测试
singleton1 = Singleton()
singleton2 = Singleton()

print(singleton1 is singleton2) # 输出 True,表明两个对象是同一个实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3. 使用元类实现单例
元类控制类的创建过程。通过重写元类的 __call__ 方法,我们可以确保类只实例化一次。

class SingletonMeta(type):
_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]

class Singleton(metaclass=SingletonMeta):
def __init__(self):
self.value = 42

# 测试
singleton1 = Singleton()
singleton2 = Singleton()

print(singleton1 is singleton2) # 输出 True,表明两个对象是同一个实例

SSH连接服务器

1
2
3
4
5
6
7
8
9
10
11
12
安装Git软件
ssh-keygen
在下面目录里找到公钥私钥
C:\Users\Administrator\.ssh
id_rsa私钥,id_rsa.pub公钥
打开vscode,安装好remote-ssh插件
设置填入ssh的Config File
C:\Users\Administrator\.ssh
将公钥放到用户目录的.ssh文件夹里
echo id_rsa.pub >> authorized_keys
使用命令连接
ssh -p xxxx username@ipaddress

linux查看cpu占用

1
2
查看cpu的占用情况,输入进程pid可以查看进程占用情况
htop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
查看cpu的占用情况,按1展开各个核心的占用情况
top

us (user space):表示用户空间的CPU使用百分比,即应用程序的CPU占用。
sy (system):表示系统空间的CPU使用百分比,即内核的CPU占用。
ni (nice):表示用户空间中调整过优先级的进程的CPU使用百分比。
id (idle):表示CPU空闲时间的百分比,即CPU没有进行任何任务的时间。
wa (iowait):表示CPU等待I/O操作完成的时间百分比。
hi (hardware interrupts):表示处理硬件中断的CPU使用百分比。
si (software interrupts):表示处理软件中断的CPU使用百分比。
st (steal):表示虚拟化环境下,其他虚拟机对CPU的占用时间百分比。

PID: 进程ID(Process ID)
USER: 进程所有者的用户名(User)
PR: 进程优先级(Priority)
NI: 进程的“nice”值(Nice value),影响进程的优先级
VIRT: 进程使用的虚拟内存总量(Virtual Memory Size)
RES: 进程使用的物理内存总量(Resident Set Size)
SHR: 进程共享的内存总量(Shared Memory Size)
S: 进程状态(State),如S代表休眠(Sleeping),R代表运行(Running)
%CPU: 进程使用的CPU百分比(CPU Usage)
%MEM: 进程使用的内存百分比(Memory Usage)
TIME+: 进程总共使用的CPU时间(CPU Time) 格式MM:SS.s
COMMAND: 启动进程的命令(Command)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
每隔一秒查看cpu使用情况 Virtual Memory Statistics
vmstat 1

r: 运行队列中等待运行的进程数(Running)
b: 在等待I/O操作的进程数(Blocked)
swpd: 使用的交换空间总量(Swap Used)
free: 空闲的内存总量(Free Memory)
buff: 用作缓存的内存总量(Buffered Memory)
cache: 用作文件缓存的内存总量(Cached Memory)
si: 从交换空间到内存的数据传输速度(Swap In)
so: 从内存到交换空间的数据传输速度(Swap Out)
bi: 从块设备读取的数据量(Block In)
bo: 写入到块设备的数据量(Block Out)
in: 每秒中断的次数(Interrupts)
cs: 每秒上下文切换的次数(Context Switches)
us: 用户空间使用的CPU百分比(User Space CPU)
sy: 系统空间使用的CPU百分比(System CPU)
id: CPU空闲百分比(Idle CPU)
wa: 等待I/O操作的CPU百分比(I/O Wait)
st: 虚拟化环境中,其他虚拟机占用的CPU百分比(Steal Time)
1
2
每隔一秒查看cpu使用情况
sar -u 1
1
2
每隔一秒查看cpu各个核心使用情况 Multiprocessor Statistics
mpstat -P ALL 1
1
2
每隔一秒查看用户空间cpu占比
pidstat -u 1