MetaProgramming

Programming Workflow

Build System

run commands for you and reslove dependencies

Make

1
2
$ make
make: *** No targets specified and no makefile found. Stop.

Makefile

1
2
3
4
5
paper.pdf: paper.tex plot-data.png
pdflatex paper.tex

plot-%.png: plot.py %.dat
./plot.py -i $*.dat -o $@

Special variables

1
2
3
$@ // the name of the target
% //The % in a rule is a “pattern”, and will match the same string on the left and on the right. (like .* in regex)
$* // result of match

Version number(Semantic versioning)

usually they are three numbers separated by dot

1
2
3
4
5
6
7
$ python3 --version
Python 3.9.7
$ python2 --version
Python 2.7.16
$ bash --version
GNU bash, version 3.2.57(1)-release
....

x.x.x

Major.Minor.Patch

  • Patch : no interface change
  • Minor : new features added (backwards compatible)
  • Major : backwards incompatible changes(some thing deleted)

Continuous Integration(CI)

Event driven system, automatically do something for you when you take some action

wiki

Daemon Processes

wiki

background programs, name ended with ‘d’, aka services

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ ps -A | grep -e 'd$'
1 ? 00:00:00 systemd
2 ? 00:00:00 kthreadd
8 ? 00:00:00 rcu_sched
16 ? 00:00:00 kauditd
17 ? 00:00:00 khungtaskd
21 ? 00:00:00 ksmd
22 ? 00:00:00 khugepaged
24 ? 00:00:00 kintegrityd
25 ? 00:00:00 kblockd
27 ? 00:00:00 md
30 ? 00:00:00 watchdogd
78 ? 00:00:00 kthrotld
447 ? 00:00:00 systemd-udevd
450 ? 00:00:00 lvmetad
453 ? 00:00:00 rpciod
454 ? 00:00:00 xprtiod
486 ? 00:00:00 rpcbind
648 ? 00:00:00 atd
670 ? 00:00:00 systemd-logind
689 ? 00:00:00 rsyslogd
706 ? 00:00:00 polkitd
954 ? 00:00:00 sshd
1801 ? 00:00:00 sshd
1803 ? 00:00:00 systemd
1891 ? 00:00:00 sshd

You can configure your own daemon program (called system unit)with a config file and systemd will manage it for you.

Say you want to start clash after boot up your computer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ cd /etc/systemd/system
$ touch clash.service
$ cat clash.service
[Unit]
Description=Clash Proxy
After=network.target

[Service]
User=vagrant
Group=vagrant
WorkingDirectory=/home/vagrant
ExecStart=/usr/bin/clash
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
3
4
$ systemctl daemon-reload
$ systemctl enable clash.service
$ systemctl start clash.service
$ systemctl status clash.service

If you want to schedule some tasks by time, cron is the daemon already there for you

You can “Query or send control commands to the systemd manager.” via systemctl.
See more at systemctl --help

simple tutorial

FUSE

Filesystem in User Space

wiki