今天在使用 journalctl 查看 nginx 日志时看到在每次启动服务后会出现一条错误信息:

$ journalctl -u nginx
...
nginx.service: Failed to parse PID from file /opt/nginx/logs/nginx.pid: Invalid argument
...

查找了下原因,可能是 nginx 在启动时创建 nginx.pid 文件前 systemd 就在请求这个文件,所以出错了。

解决办法就是题前手动创建 systemd 需要的文件:

mkdir /etc/systemd/system/nginx.service.d
printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
systemctl daemon-reload

以上处理就可以解决问题。

参考链接:
https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864



awk 是常用的 Linux 文本操作命令和脚本语言。用来按行提取和处理文本内容,也可以执行简单的逻辑处理。

比如我们有一个 txt 文件:

ab.c 123 e.rt 456
oh.g 324 b.na 756
si.d 156 o.ui 452

阅读全文


执行 shell 脚本时经常会有传入参数,如:

./test.sh abcdef abc.bbb

以上的命令使用了两个传入参数,abcdef,abc.bbb

在脚本里使用时,$1 就表示第一个参数,$2 就表示第二个参数:

var1 = $1
var2 = $2

在脚本中有一种用法,如:${1%def}jjj
他的意思就是将 $1 最后的字符 def 替换为 jjj

newstr1 = ${1%def}jjj

newstr1 的值就是 abcjjj

newstr2 = ${2%.bbb}.ccc

newstr2 的值就是 abc.ccc


语法:

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

执行cmd命令并返回结果到字符串。

用法:

import subprocess

output = check_output(["cat", "/etc/hostname"]).strip()
print(output)

以上脚本会执行 cat /etc/hostname 命令然后将结果赋值给 output 变量。
strip() 可以将 string 的前后空格去掉。