西维蜀黍

【Linux】Crontab

区别

Cron: Cron comes from chron, the Greek prefix for ‘time’. Cron is a daemon which runs at the times of system boot.

Crontab: Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times. File location varies by operating systems.

  • crontab是一个文件(列举了要执行的cron服务的具体内容),也可以指Linux中的crontab 命令。

Cron job or cron schedule: Cron job or cron schedule is a specific set of execution instructions specifying day, time and command to execute. crontab can have multiple execution statements.

crond 系统服务

crond系统服务提供crontab命令来设定cron服务的。

在linux 系统中,由 crond这个系统服务来控制。Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的。

我们可以看下crond这个系统服务的状态:

$ sudo systemctl status cron
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-03-22 16:39:57 UTC; 39s ago
       Docs: man:cron(8)
   Main PID: 4344 (cron)
      Tasks: 1 (limit: 4626)
     Memory: 348.0K
        CPU: 41ms
     CGroup: /system.slice/cron.service
             └─4344 /usr/sbin/cron -f -P

Mar 22 16:39:57 esxiubuntu systemd[1]: Started Regular background program processing daemon.
Mar 22 16:39:57 esxiubuntu cron[4344]: (CRON) INFO (pidfile fd = 3)

启动crontab服务

$ systemctl start cron

查看服务是否已经运行用

$ ps -ax | grep cron

设置cron服务的两种方法

  • 在命令行中使用 crontab 来管理服务(添加/删除相应的任务),该命令其实会将设置的cron服务存储到 /var/spool/cron/ 目录下(以用户名作为子文件夹名称)
  • 直接编辑/etc/crontab文件,以添加相应的任务
  ...


【WordPress】使用 Docker 创建 WordPress 实例

Create docker-compose.yml

Change into your project directory.

For example, if you named your directory my_wordpress:

cd my_wordpress/

Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with a volume mount for data persistence:

version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  ...


【AWS】AWS CLI 使用

Install AWS CLI V2

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
$ sudo installer -pkg AWSCLIV2.pkg -target /

Verify the installation

To verify that the shell can find and run the aws command in your $PATH, use the following commands.

$ which aws
/usr/local/bin/aws 
$ aws --version
aws-cli/2.0.36 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0
  ...


【AWS】s3cmd 使用

Install

Windows

直接执行.py源文件

Install Python

Download and install python 3

https://www.python.org/downloads/

After installing python, append the Python in Path environment variable. Open command prompt and verify python version.

C:> python3 --version
Python 3.9.4

Download s3cmd

$ git clone https://github.com/s3tools/s3cmd.git

# 解压

$ cd s3cmd

$ python3 s3cmd --configure

Verify

$ python3 s3cmd ls

2014-02-03 06:37  s3://tecadmin
2014-03-29 07:56  s3://tecadminbackups
  ...


【Compile】交叉编译器(Cross Compiler)

本地编译

本地编译可以理解为,在当前编译平台下,编译出来的程序只能放到当前平台下运行。平时我们常见的软件开发,都是属于本地编译:

比如,我们在 x86 平台上,编写程序并编译成可执行程序。这种方式下,我们使用 x86 平台上的工具,开发针对 x86 平台本身的可执行程序,这个编译过程称为本地编译。

交叉编译(Cross compiler)

交叉编译可以理解为,在当前编译平台下,编译出来的程序能运行在体系结构不同的另一种目标平台上,但是编译平台本身却不能运行该程序:

比如,我们在 x86 平台上,编写程序并编译成能运行在 ARM 平台的程序,编译得到的程序在 x86 平台上是不能运行的,必须放到 ARM 平台上才能运行。

为什么会有交叉编译

之所以要有交叉编译,主要原因是:

  • Speed: 目标平台的运行速度往往比主机慢得多,许多专用的嵌入式硬件被设计为低成本和低功耗,没有太高的性能
  • Capability: 整个编译过程是非常消耗资源的,嵌入式系统往往没有足够的内存或磁盘空间
  • Availability: 即使目标平台资源很充足,可以本地编译,但是第一个在目标平台上运行的本地编译器总需要通过交叉编译获得
  • Flexibility: 一个完整的Linux编译环境需要很多支持包,交叉编译使我们不需要花时间将各种支持包移植到目标板上
  ...


【Linux】Ubuntu 安装 Docker

Installation

Method 1 - Convenience Script

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
<...>

If you would like to use Docker as a non-root user,

Approach 1

$ sudo sh -eux <<EOF
# Install newuidmap & newgidmap binaries
apt-get install -y uidmap
EOF

$  dockerd-rootless-setuptool.sh install

# Verify that you can run docker commands without sudo.
$ docker run hello-world

Approach 2

you should now consider adding your user to the “docker” group with something like:

$ sudo usermod -aG docker ${USER}

# 将当前用户切换到docker组中,且立即生效
$ newgrp docker

# Verify that you can run docker commands without sudo.
$ docker run hello-world
  ...


【Linux】Shell

判断 - if

#!/bin/bash
# Basic if statement
if [ $1 -gt 100 ]; then
  echo Hey that\'s a large number.
  pwd
fi
  ...


【Linux】Shell - 变量

定义变量

定义变量时,变量名不需要加美元符号($),如:

variableName="value"

注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样。同时,变量名的命名须遵循如下规则:

  • 首个字符必须为字母(a-z,A-Z)。
  • 中间不能有空格,可以使用下划线(_)。
  • 不能使用标点符号。
  • 不能使用 bash 里的关键字(可用 help 命令查看保留关键字)。

Bash 没有数据类型的概念,所有的变量值都是字符串。

下面是一些自定义变量的例子。

# 不使用任何引号
$ myGender=man
# 使用单引号
$ myName='Wei Shi'
# 使用双引号
$ myUrl="http://test.com"
# 可以将一个数字赋值给变量(但是这个变量仍然是一个字符串变量)
$ myNum=100
  ...


【Linux】makefile

Make的概念

Make这个词,英语的意思是"制作"。Make命令直接用了这个意思,就是要做出某个文件。比如,要做出文件a.txt,就可以执行下面的命令。

$ make a.txt

但是,如果你真的输入这条命令,它并不会起作用。因为Make命令本身并不知道,如何做出a.txt,需要有人告诉它,如何调用其他命令完成这个目标(这个人就是我们)。

比如,假设文件 a.txt 依赖于 b.txt 和 c.txt ,是后面两个文件连接(cat命令)的产物。那么,make 需要知道下面的规则。

# makefile 文件内容
a.txt: b.txt c.txt
    cat b.txt c.txt > a.txt

也就是说,make a.txt 这条命令的背后,实际上分成两步:第一步,确认 b.txt 和 c.txt 必须已经存在,第二步使用 cat 命令 将这个两个文件合并,输出为新文件。

像这样的规则,都写在一个叫做Makefile的文件中,Make命令依赖这个文件进行构建。Makefile文件也可以写为makefile, 或者用命令行参数指定为其他文件名:

$ make -f rules.txt
# 或者
$ make --file=rules.txt

上面代码指定make命令依据rules.txt文件中的规则,进行构建。

总之,make只是一个根据指定的Shell命令进行构建的工具。它的规则很简单,你规定要构建哪个文件、它依赖哪些源文件,当那些文件有变动时,如何重新构建它。

  ...


【MySQL】Out of range value for columns

Reproduce

drop table if exists test_tinyint;
create table test_tinyint (
    num tinyint
) engine=innodb charset=utf8;

insert into test_tinyint values(-100);
insert into test_tinyint values(255);

执行第7行的代码时候报错"Out of range value for column ’num’ at row 7",这其实是因为(Signed 的) TINYINT 的范围是 -128 到 127,而 255 超过了这个范围。

如果把第3行的num字段定义改为"num tinyint unsigned",第7行的插入就不会报错了。但是第6行的插入-100又报错了,因为无符号整型是无法表示负数的。

Reference

  ...