Skip to main content

常用命令集合

Ali服务器命令

composer 挂代理

export HTTP_PROXY=http://customer-xxxx-cc-MY:123456@pr.oxylabs.io:7777
export HTTPS_PROXY=http://customer-xxxx-cc-MY:123456@pr.oxylabs.io:7777
curl -v https://ipinfo.io

nginx

systemctl stop nginx
systemctl start nginx
nginx -t
nginx -s reload

php

#1 s-api
systemctl restart php7.3-fpm

git

# 撤销master分支合并 还原到合并前的一个版本
git reset --hard b316ac6d6f6643c589e9c2a46653cae94c61b878
# 强制推送到master分支
git push --force
# 重置本地仓库并将远程同步到本地
git fetch origin
git reset --hard origin/<branch-name>
git pull

#1 普通更新
su -c "git diff" www-data
su -c "git log" www-data
su -c "git checkout . && git pull && composer dumpautoload && gulp" www-data
#2 大更新
su -c "git checkout . && git checkout master && git pull" www-data
su -c "composer update && npm install && gulp" www-data
#3 子命令
su -c 'git pull' www-data
su -c "git checkout ." www-data
su -c "gulp" www-data
su -c "cd /var/www/s-store && composer update" www-data

#4 Kchat-Swoft
su -c "git pull && php bin/swoft ws:restart" www-data
su -c 'git pull' www-data
su -c "php bin/swoft ws:restart" www-data
su -c "php bin/swoft ws:stop" www-data
su -c "php bin/swoft ws:start" www-data
su -c "php bin/swoft ws:start -d" www-data
su -c "php bin/swoft http:stop" www-data
su -c "php bin/swoft http:start" www-data
for i in `ps -ef|grep swoft-ws |awk '{print $2}' `; do kill -9 $i ; done;
for i in `ps -ef|grep swoft-http |awk '{print $2}' `; do kill -9 $i ; done;

#4 ms-message
su -c "git pull & php bin/swoft http:restart" www-data
php bin/swoft http:restart

安装软件

apt install npm

定时任务和脚本

# 查看定时任务
su -c "crontab -l" www-data
# 添加|编辑定时任务 方式1
su -c "crontab -e" www-data
# 添加|编辑定时任务 方式2
su -c "crontab task_prod.cron" www-data
# task_prod.cron 内容:
# clear-cache
*/30 * * * * php /var/www/h-core-api/console.php clear-cache --env=prod
* * * * * php /var/www/h-core-api/console.php clear-expired-top-up --env=prod
0 1 * * * php /var/www/h-core-api/console.php stats-pin-data --env=prod
0 3 1 * * php /var/www/h-core-api/console.php stats-merchant-transaction --env=prod
0 1 * * * php /var/www/h-core-api/console.php handle-expired-card --env=prod


# Transferto country & opeartor update 更新:
cd /var/www/s-cpanel/public/
php index.php --env prod --route seo/update-recharge-country
php index.php --env prod --route seo/update-recharge-operator

# 手动更新用户子账户余额报表 s-task
php sum_user_credits.php env=dev date=2020/03/29
php sum_user_credits.php env=test date=2020/03/29
php sum_user_credits.php env=prod date=2020/03/29

# 更新FL产品列表
php public/index.php --env prod --route card/cacheFL

常用命令集合

执行字符串命令

#1 使用 `eval` 命令,格式如下:eval "命令字符串"
str_cmd="echo hello"
eval $str_cmd
#2 使用 `bash -c` 命令,格式如下:bash -c "命令字符串"
str_cmd="echo hello"
bash -c "$str_cmd"

Cat命令

#1 搜索关键字 显示前后5行
cat log.txt | grep 'ERROR' -C 5
#2 搜索关键字 显示后5行
cat log.txt | grep 'ERROR' -A 5
#3 mac快速查找
mdfind -name my.cnf
#4 实时查看文件尾部内容
tail -f logfile.log

查看进程/内存/磁盘空间

#1 centos 查看文件可用空间:
df -h
#2 查看当前目录文件夹大小:
du -h --max-depth 1
#3 查看当前文件列表大小:
ls -lh
#4 查看cpu/内存使用率(执行完成后 按“M”安装内存倒序,按“P”按照CPU使用率倒序):
top
#5 查看内存剩余:
free -m
#6 通过ps查看内存和cpu:
ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'

端口/进程查看和管理

#1 查看端口占用
sudo lsof -i :1086
lsof -i tcp:1086
#2 ps查看进程
ps aux |grep -e 'USER' -e 'nginx' -e 'php'
ps -ef|grep nginx
ps -ef|grep php-fpm
history | grep ssh
#3 ps查看进程(PID/Name)
sudo pkill -9 541
sudo pkill -9 php-fpm

zsh 查看历史命令

#1 查看所有历史命令
history 1 -1
#2 搜索相关历史命令
history 1 -1 | grep php
history 1 -1 | grep pecl
history 1 -1 | grep brew

代理设置(暂时的)

#1 开启代理
export http_proxy=127.0.0.1:1087
export https_proxy=127.0.0.1:1087
#2 关闭代理
unset http_proxy
unset https_proxy

git代理设置(长期的)

#1 查看全局配置
git config --global --list
#2 设置
git config --global http.proxy http://127.0.0.1:1087
git config --global https.proxy https://127.0.0.1:1087
#3 取消设置
git config --global --unset http.proxy
git config --global --unset https.proxy

文件管理

缓存文件夹清理(s-store/tmp为例)

    #1 将tmp中超过15天的文件 移动到 s-store-copys/tmp文件夹中命令:
cd /xxx/xxx/s-store/tmp
sudo find ./ -name "*.png" -mtime +15 -exec mv {} ../../s-store-copys/tmp \;
sudo find ./ -name "*.jpg" -mtime +15 -exec mv {} ../../s-store-copys/tmp \;
sudo find ./ -name "*.jpeg" -mtime +15 -exec mv {} ../../s-store-copys/tmp \;
sudo find ./ -name "*.PNG" -mtime +15 -exec mv {} ../../s-store-copys/tmp \;
sudo find ./ -name "*blob" -mtime +15 -exec mv {} ../../s-store-copys/tmp \;
sudo find ./ -name "*.JPG" -mtime +15 -exec mv {} ../../s-store-copys/tmp \;

打包压缩

  • 命令文档:【Linux】tar压缩解压缩笔记 - 爱啦啦 - 博客园

  • 压缩单个文件:

    tar -zcvf log.tar.gz 20_02_17Log.log
  • 压缩批量文件:

    #1. 将3天以外的log 移动到history
    sudo find ./ -maxdepth 1 -name "*.log" -mtime +3 -exec mv {} history \;
    #2. 去到history
    cd history/
    #3. 压缩打包
    tar -zcvf 20200117_20200215log.tar.gz *.log
    #4. 删除history中的log
    rm -rf *.log
    #5. 查看剩余空间
    df -h

批量移动/删除文件:

#1 批量删除文件 删除60天之外的所有.log文件:
sudo find ./ -maxdepth 1 -name "*.log" -mtime +60 -exec rm -rf {} \;
#1 清空文件内容 swoole.log文件:
su -c "> swoole.log" www-data
#2 find 命令
#2.1 当前目录下 .log 结尾的文件: ./ -name "*.log"
#2.2 60天之外的: -mtime +60
#2.3 对查找结果 执行的操作: -exec rm -rf {} \
#3 移动15天之外的日志:
sudo find ./ -maxdepth 1 -name "*.xls" -mtime +7 -exec mv {} history \;
sudo find ./ -maxdepth 1 -name "*.log" -mtime +15 -exec mv {} ./histories \;

find 命令介绍:mac 显示隐藏文件+Find命令

移动、复制、删除

mv k-store k-store.bak
mv k-store.20180727 k-store
rm -rf node_modules
cp k-api k-api.20180730 -r
cp -r s-api copy-s-api/s-api-0701

空间查看

#1 centos 查看文件可用空间:
df -h
#2 查看当前目录文件夹大小:
du -h --max-depth 1
#3 查看当前文件列表大小:
ls -lh

权限更改

#1 修改文件夹权限
chmod ug+rwx file;
chmod 771 file;
#2 修改所有者
chown www-data: filedir -R;
3# 修改可执行
chown u+x file;

文件上传下载

#1 远程下载文件:
scp -P22 centos@52.74.1xx.1xx:/xxx/xxx/s-pay/logs/k_call.log ~/Downloads/k-call.log
scp -P22 centos@s-cpanel:/xxx/xxx/xxx/s-cpanel.access.log-20190318.gz ~/Downloads/cpanel.log-0318.gz
#2 远程下载文件夹
scp -r -P22 centos@52.74.1xx.1xx:/xxx/xxx/s-store/web/skin/js/data ~/Downloads/data
scp -P22 centos@s-cpanel:/xxx/xxx/xxx/xxx/32DFE214-0971-3497-6D1B-EB745D72E18A.xlsx ~/Downloads/cas.xlsx
#3 远程上传文件

php:

重启:

sudo service php-fpm restart
sudo systemctl restart php-fpm
# 正式服:
systemctl restart php-fpm
# 正式服:
history|grep php
systemctl restart php7.1-fpm

常用调试代码

//1 设置超时时间
set_time_limit(120);
//2 设置内存大小
ini_set('memory_limit', '512M');
//3 设置错误打印
ini_set('display_errors', 'On');
error_reporting(E_ALL);
//4 打印错误并结束
die("test");
print_r($result);
die;

systemctl:

命令详解:

##列出当前系统服务的状态
systemctl list-units
##列出服务的开机状态
systemctl list-unit-files
##查看指定服务的状态
systemctl status sshd
##关闭指定服务
systemctl stop sshd
##开启指定服务
systemctl start sshd
##从新启动服务
systemctl restart sshd
##设定指定服务开机开启
systemctl enable sshd
##设定指定服务开机关闭
systemctl disable sshd
##使指定服务从新加载配置
systemctl reload sshd

npm

npm常用命令

#1 查看安装列表 ls 、list
npm ls -g
npm list -g --depth 0
npm ls
#2 搜索:https://docs.npmjs.com/cli-commands/view.html
npm search gulp
npm view gulp
npm version
#3 安装
npm install gulp
npm install gulp -g
#4 更新
npm update gulp
#5 删除
npm uninstall gulp
npm uninstall gulp -g
#6 清除缓存
npm cache clean

npm package管理

#1 初始化 生成package.json
npm init
#2 安装并添加到 package
npm install gulp --save
#3 直接通过package.json安装
npm install

npm 代理设置(长期的)

#1 开启代理
npm config set proxy http://127.0.0.1:1087
npm config set proxy http://127.0.0.1:1087
#2 查看:
npm config list
#3 关闭代理
npm config delete proxy
npm config delete https_proxy

nginx

  • 校验配置:nginx -t

  • 启动: nginx

  • 重启:

    sudo nginx -s reload
    sudo service nginx restart
    sudo systemctl start nginx
    sudo systemctl restart nginx
    systemctl status nginx.service
    ps aumx|grep nginx
    sudo kill 30443
    sudo killall nginx

git:

git reset -hard 21e4615ac29c880171ccb7454b91b08c07e56e65

其他

systemctl restart metricbeatt
systemctl restart metricbeat
systemctl restart filebeat

SSH配置设置

常用ssh命令

# 1 corp项目更新
ssh s-corp -t "cd /var/www/s-corp && pwd && su -c 'git pull && composer dumpautoload && gulp' www-data"
# 2 GeoIP库下载到oss
ssh s-store -t 'cd /usr/share/GeoIP/ && ls -lh && cd .. && ossutil64 cp -r GeoIP/ oss://s-copys2/ecs-logs/GeoIP/'
# 3 lc-hyperf项目更新
ssh kc-v4 -t "cd /var/www/lc-hyperf/ && pwd && git pull && ./restart.sh" && sleep 3 && ssh kchat-v4 -t "cd /var/www/lc-hyperf/ && pwd && php bin/hyperf.php start"
# 4 lc-hyperf项目日志打包
ssh kc-v4 -t "cd /var/www/lc-hyperf/runtime/ && tar -zcvf logs_2024091402.tar.gz ./logs/ && ossutil cp logs_2024091402.tar.gz oss://s-copys2/logs/kc/logs_2024091402.tar.gz"
# 5 lc-hyperf项目更新【Dev环境】
ssh s-dev -t "cd /var/www/lc-hyperf/dev/ && pwd && git pull && ./restart-dev.sh" && sleep 5 && ssh s-dev -t "cd /var/www/lc-hyperf/dev/ && pwd && php8.2 bin/hyperf.php start"

常用shell命令