Linux systemd unit 配置文件

基础概念可参考阮一峰写的 Systemd 定时器教程

[Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下。

  • Type:定义启动时的进程行为。它有以下几种值。
  • Type=simple:默认值,执行ExecStart指定的命令,启动主进程
  • Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
  • Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
  • Type=dbus:当前服务通过D-Bus启动
  • Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
  • Type=idle:若有其他任务执行完毕,当前服务才会运行
  • ExecStart:启动当前服务的命令
  • ExecStartPre:启动当前服务之前执行的命令
  • ExecStartPost:启动当前服务之后执行的命令
  • ExecReload:重启当前服务时执行的命令
  • ExecStop:停止当前服务时执行的命令
  • ExecStopPost:停止当其服务之后执行的命令
  • RestartSec:自动重启当前服务间隔的秒数
  • Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-successon-failureon-abnormalon-aborton-watchdog
  • TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
  • Environment:指定环境变量

Unit 配置文件的完整字段清单,请参考官方文档

Python之禅

如何显示?

import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

中文翻译:

JS 生成随机字符串

Generate random characters

Math.random().toString(36).replace(/[^a-z]+/g, '').slice(0, 5);

Math.random(),生成随机数,例如:0.09751664076957856
.toString(36),转换成36进制后的字符串。输出类似:“0.3idqid3cvvy”
.replace(/[^a-z]+/g, '') ,仅保留小写自负。输出类似:“idqidcvvy”
.slice(0, 5), 截取前5个字符。输出类似:"idqid"

Python 遍历指定目录下的文件

列出指定目录下的全部文件,或可以通过扩展名指定文件类型,也可以通过指定排除规则,忽略部分文件。

def list_files(
    dir: str, ext: list = None, recursive: bool = True, excludes: list = None
):
    """
    Args:
        - dir, directory path.
        - ext, file extension list, lowercase letters. Default is None, which means all files.
        - recursive, Default is True, will list files in subfolders.
        - excludes, exclude folder or file name list, regexp pattern string. Default is None, which means all folders

    Tips:
        - How to get relative path of a file: os.path.relpath(file_path, dir_path)
        - How to get only name of a file: os.path.basename(file_path)

    Version:
        v0.2.0
        https://gist.github.com/nodewee/eae12e2b74beb82162b8b488648f1fdd
    """

    for f in os.scandir(dir):
        if excludes:
            is_ignore = False
            for pat in excludes:
                if re.search(pat, f.name):
                    is_ignore = True
                    break
            if is_ignore:
                continue

        if recursive:
            if f.is_dir():
                for item in list_files(f.path, ext, recursive, excludes):
                    yield item

        if f.is_file():
            if ext is None:
                yield f.path
            else:
                if os.path.splitext(f.name)[1].lower() in ext:
                    yield f.path

Code on GitHub Gist

Python 复制文本到剪贴板

目前(Python 3.8)为止,Python 并没有原生支持访问系统剪贴板。

搜寻出有几种方法,梳理如下:

简单有效。但是并不适合复杂环境。

import os
# on macOS
def write_to_clipboard(text):
    command = f"echo '{text}' | pbcopy"
    os.system(command)

def read_from_clipboard():
    command = "pbpaste"
    return os.popen(command).read()

更多 Shell 命令参考:Shell use clipboard to copy and paste

Qt Pyside2 访问剪贴板

Qt/Pyside2 读取系统剪贴板内容

import sys
from PySide2.QtWidgets import QApplication

app = QApplication(sys.argv)
clipboard = app.clipboard()

print(clipboard.mimeData().formats())
print(clipboard.mimeData().data(clipboard.mimeData().formats()[0]))

app.closeAllWindows()
app=None

mimeData() 就和 drag 时对 mimeData() 的操作一样了。

引用自 doc.qt.io:

QClipboard supports the same data types that QDrag does, and uses similar mechanisms. For advanced clipboard usage read Drag and Drop .