create_ln.py同步nginx中两个文件夹的内容
在nginx的sites-avaliable中,增加新文件之后,需要创建软连接到sites-enable里,reload才会生效,所以写了如下脚本:
- create_ln.py
#!/usr/bin/env python3
import os
from pathlib import Path
def run_and_echo(cmd: str) -> int:
print('-->', cmd)
return os.system(cmd)
def main():
pwd = Path(__file__).resolve().parent
ps = pwd.glob('*.*')
target = pwd.parent / 'sites-enabled'
count = 0
for i in ps:
name = i.name
p = target.joinpath(name)
if name.endswith('.bak') or name.endswith('.py') or p.exists():
continue
print(f'creating ln of {name} ...')
run_and_echo(f'sudo ln -s {i} {p}')
count += 1
if not count:
print('No need to create soft link.')
if __name__ == '__main__':
main()
Usage::
chmod +x create_ln.py
sudo mv create_ln.py /etc/nginx/sites-available/
cd /etc/nginx/sites-available/
./create_ln.py
sudo nginx -t