0%

关于终端配置的一些记录


mac自带的sed和linux表现不一致

需要安装 gnu-sed, 推荐 Homebrew 安装

1
2
3
4
5
6
7
8
9
10
11
# 安装gnu-sed
brew install gnu-sed

# 编辑zsh配置文件
vi ~/.zshrc

# 设置PATH
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

#更新配置
source ~/.zshrc

再试,就可以了!

1
2
3
4
5
echo "a,b,c,d" |sed  's/,/\n/g'
a
b
c
d

npm换源

更换镜像即可解决,以淘宝的镜像为例:

1. 临时使用

1
npm --registry https://registry.npm.taobao.org install express

验证是否成功

1
npm info express

2. 持久使用

1
npm config set registry https://registry.npm.taobao.org

验证是否成功

1
npm config get registry

3. 恢复使用官方镜像

1
npm config set registry https://registry.npmjs.org

pip换源

国内的一些源:

名称 地址
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

1. 临时使用

在使用pip的时候在后面加上-i参数,指定pip源,例如:

1
pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

2.永久修改

修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:

1
2
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

mac 安装Homebrew报错

安装Homebrew时有时会出现:

1
Failed to connect to raw.githubusercontent.com port 443: Connection refused

这时,解决办法也很简单,就是配置一个代理,把下面的内容配置到你的 /etc/hosts 中即可解决

1
199.232.28.133 raw.githubusercontent.com

CentOS 相关命令

防火墙相关:

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
#启动防火墙
systemctl start firewalld.service

#关闭防火墙
systemctl stop firewalld.service

#开机启动防火墙
systemctl enable firewalld.service

#禁用开机启动防火墙
systemctl disable firewalld.service

#重启防火墙
firewall-cmd --reload

#显示防火墙开放的端口
firewall-cmd --list-ports

#查询端口是否开放
firewall-cmd --query-port=80/tcp

#开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent

#停止端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent

查看端口访问情况:

1
2
3
4
5
6
7
8
9
10
11
#列出所有端口情况
netstat -ntlp

#查看某个端口被哪个进程占用
netstat -lnp|grep 443

#查看进程的详细信息
ps 1777

#终止进程
kill -9 1777

apache命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#编辑Apache配置文件
vim /etc/httpd/conf/httpd.conf

#增加转发配置
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

ProxyPass /wx http://0.0.0.0:8080/wx
ProxyPassReverse /wx http://0.0.0.0:8080/wx

#启动
systemctl start httpd

#停止
systemctl stop httpd

#重启
systemctl restart httpd

#查看状态
status httpd.service

后台运行:

1
2
3
4
5
#在后台执行 root 目录下的 runoob.sh 脚本
nohup /root/runoob.sh &

#查找到 nohup 运行脚本到 PID
ps -aux | grep "runoob.sh"

软链接报错相关

若出现类似下面的报错,可以尝试修复软连接来解决:

1
2
$ /usr/local/bin/python3 --version
no such file or directory: /usr/local/bin/python3

解决方法:

1
2
3
4
5
6
#先移除软链接
cd /usr/local/bin/
rm python3

#建立新的软链接
ln -s /usr/local/opt/python@3.8/bin/python3 /usr/local/bin/python3

测试一下是否可用:

1
2
$ /usr/local/bin/python3 --version
Python 3.8.6

Homebrew更新相关

如果你之前折腾过不少导致你的Homebrew有点问题,那么可以尝试使用如下方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 诊断Homebrew的问题:
brew doctor

# 重置brew.git设置:
cd "$(brew --repo)"
git fetch
git reset --hard origin/master

# homebrew-core.git同理:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git fetch
git reset --hard origin/master

# 应用生效:
brew update

关闭brew执行命令时自动更新:

1
2
# 在 ~/.bash_profile 或者 ~/.zshrc 新增一行
export HOMEBREW_NO_AUTO_UPDATE=true

Crontab定时任务

以定时启动爬虫为例

1
2
3
4
5
6
7
# 查看命令得绝对路径
$which scrapy
/usr/local/bin/scrapy

# cd到爬虫得项目目录下 + scrapy命令得绝对路径 + 启动命令
# 每5分钟执行一次iTorrents这个爬虫程序
*/5 * * * * cd /home/iSpiders && /usr/local/bin/scrapy crawl iTorrents >>/home/log/iTorrents.log 2>&1 # 将正确和错误日志都打印到日志中

命令详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#创建定时任务
crontab -e

#查看定时任务
crontab -l

#停止定时任务
service crond stop

#重启定时任务
service crond restart

#启动定时任务
service crond start

crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有:
* 取值范围内的所有数字
/ 每过多少个数字
- 从X到Z
, 散列数字

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
#每1分钟执行一次cmd
* * * * * cmd

#每小时的第3和第15分钟执行
3,15 * * * * cmd

#在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * cmd

#每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * cmd

#每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 cmd

#每晚的21:30执行
30 21 * * * cmd

#每月1、10、22日的4 : 45执行
45 4 1,10,22 * * cmd

#每周六、周日的1 : 10执行
10 1 * * 6,0 cmd

#每天18 : 00至23 : 00之间每隔30分钟执行
0,30 18-23 * * * cmd

#每星期六的晚上11 : 00 pm执行
0 23 * * 6 cmd

#每一小时重启smb
* */1 * * * cmd

#晚上11点到早上7点之间,每隔一小时执行
* 23-7/1 * * * cmd