定时任务

crontab修改默认编辑器为vim

  1. 设置

    1
    
    export VISUAL=vim
    
  2. 使用命令select-editor

    输入 select-editor 后会显示下面的内容,输入2选择vim编辑器

    1
    2
    3
    4
    5
    6
    7
    
     Select an editor.  To change later, run 'select-editor'.
     1. /bin/nano        <---- easiest
     2. /usr/bin/vim.basic
     3. /usr/bin/vim.tiny
     4. /bin/ed
    
     Choose 1-4 [1]:
    

添加随系统启动时的任务

1
2
# 系统重启时启动 /prod/backup/backup.sh 脚本
@reboot /bin/bash /prod/backup/backup.sh >> /dev/null 2>&1

设置定时任务

打开定时任务脚本

1
crontab -e

添加定时任务

1
2
# 每天3:30执行/prod/backup/backup.sh脚本
30 3 * * * /bin/bash /prod/backup/backup.sh >> /dev/null 2>&1

后台执行命令

使用 nohup 将需要执行的命令放到后台运行,退出终端页不会影响程序的运行。例如后台运行一个start.sh启动脚本。

1
2
# & 符号表示让命令在后台运行,终端退出后命令依旧执行。
nohup ./start.sh &

shell脚本

使用sh时,定义方法写法:

1
2
3
4
# 不能写成 function hello()
hello() {

}

Ubuntu 服务器 root 密码修改

使用命令 sudo passwd

1
sudo passwd

按照终端的提示输入新密码

查看服务pid

  1. 使用 ps 命令

    1
    2
    
    # server_name 为对应的服务名称
    ps aux|grep server_name
    
  2. 使用 pgrep 命令

    1
    2
    
    # server_name 为对应的服务名称
    pgrep server_name
    

查看本机端口

查看端口占用情况,可以使用 lsof 和 netstat 命令。lsof是一个查看当前系统打开文件的工具,netstat 是一个查看网络状态的工具。

netstat

  • 查看当前所有 tcp 端口

    1
    
    netstat -at
    
  • 查看当前所有 udp 端口

    1
    
    netstat -au
    

详细参数请通过 netstat --help 查看。

lsof

  • 查看所有 tcp 相关端口

    1
    
    lsof -i tcp
    

更多参数请通过 lsof --help 查看。

使用 scp 传输文件

从本地往远程传输文件

1
scp local_file remote_username@remote_ip:remote_path

指定端口号传输

使用 -P 参数

1
scp -P remote_port local_file remote_username@remote_ip:remote_path 

从远程往本地传输文件

1
scp remote_username@remote_ip:remote_path local_file

ssh key

generate ssh key

generate ssh key with ed25519

1
ssh-keygen -t ed25519 -f example_ed25519  -C "example@email.com"

copy key to remote server

use ssh-copy-id

1
ssh-copy-id -i ~/.ssh/example_ed25519.pub remote_username@remote_ip

copy pub key and add to the remote server authorized_keys file