Django parse a json/dictionary reminder on template

如果想在一个Django template里引入json, 正确的template写法为

python的views.py的写法

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import Http404, HttpResponse

def get_product(request):
context1 = {}
context1[‘env’] = ‘production’
context1[‘mydict’] = {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4"}
context1[‘days’] = [1,2,3]

if request.GET.get("item") == "ec2" or not request.GET.get("item"):
return render(request, ‘main_content.html’, context1)
else:
raise Http404()

main_content.html使用extends引入index.html

{% extends 'index.html' %}
{% block content %}
{% for day in days %}
<li>day: {{ day }}</li>
{% endfor %}
{% endblock %}

index.html使用include引入sidebar.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="header">My page</div>
<div class="content">
<div class="sidebar">

<ul>
{% if env == 'production' %}
{% include 'production.sidebar.html' %}
{% elif env == 'development %}
{% include 'development.sidebar.html' %}
{% elif env == 'staging' %}
{% include 'staging.sidebar.html' %}
{% elif env == 'legacy' %}
{% include 'legacy.sidebar.html' %}
{% endif %}
</ul>

</div>
<div class="maincontent">

{% block content %} {% endblock %}

</div>
</div>
</body>
</html>

(more…)

Read More

How to run a crontab job every X seconds

如果你有一个job, 需要每隔5秒运行一次.

以下写法是错误的

* * * * * sleep 5; /bin/cmd

如果使用这种写法, 会导致在每分钟的第0秒开始, sleep 5秒以后, 再运行/bin/cmd, 然尔运行完就结束了, 不会每隔5秒运行一次.

正确写法1

* * * * * for i in {1..12}; do /bin/cmd; sleep 5; done

正确写法2

* * * * * REMAIN=60 INC=5 ; while ; do /bin/cmd; sleep $INC; REMAIN=$(($REMAIN - $INC)); done

正确写法3

* * * * * /bin/cmd
* * * * * sleep 5; /bin/cmd
* * * * * sleep 10; /bin/cmd
* * * * * sleep 15; /bin/cmd
......
* * * * * sleep 55; /bin/cmd

可以使用systemctl status cron.service -l命令看看cron服务的状态以及最新日志.

参考: How to run a cronjob every X seconds in cron

Read More

docker基础知识(一)

1. 安装docker

# 适用ubuntu 16.04
$ sudo apt install docker docker.io

# 适用CentOS 7 (这部分文档来自Docker官方https://docs.docker.com/install/linux/docker-ce/centos/)
$ yum install -y yum-utils device-mapper-persistent-data lvm2    # 安装依赖
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ yum install docker-ce docker-ce-cli containerd.io
$ systemctl start docker

2. Images管理

搜索kms相关的image

$ sudo docker search kms
NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
luodaoyi/kms-server                          kms server                                      21                   [OK]
lunker/kms                                   ubuntu image repositoiry for kms                1                    
leon0516/py-kms                              this is beta version!!!                         1                    [OK]
captbrick/py-kms                             Py-KMS                                          1                    [OK]
gkuzet/kms                                   KMS Server written in C                         1                    
openvidu/openvidu-server-kms                 OpenVidu platform service. It includes Ope...   1                    

比如第1个luodaoyi/kms-server,也可以用https://hub.docker.com/r/luodaoyi/kms-server/的方式查看相关信息.

下载想要的image(不必需)

$ sudo docker pull luodaoyi/kms-server

查看本地的images

$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
luodaoyi/kms-server   latest              4f86d964ac52        4 months ago        4.09 MB

$ sudo docker image ls
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
luodaoyi/kms-server   latest              4f86d964ac52        4 months ago        4.09 MB
(more…) Read More

Linux get DevName of a directory

Linux系统下, 获得某一个子目录(例如/data/home/release )对应的DevName(如sdb等). 现在的情况是

有些机器是这样的

Filesystem      Size  Used Avail Use% Mounted on
......
/dev/sda5       1.6T  343G  1.2T  23% /data

而有些机器是这样的

Filesystem      Size  Used Avail Use% Mounted on
......
/dev/sda5       1.6T  343G  1.2T  23% /data
/dev/sdb        488M   56M  408M  12% /data/home/release

解决办法

part=`df -P /data/home/release | awk 'END{print $1}'`    #得到一个诸如/dev/sda3的字符串
devname=${part:5:3}             #切割上面的字符串,从第5位开始,取3位
echo $devname
Read More