---
title: "dkron - cronやめたい | grasys blog"
url: "https://blog.grasys.io/post/yusukeh/dkron"
description: "こんにちは！grasys長谷川です！ cron・・・めっちゃ困るcron・・・ 古いぜcron・・・もういやだcron・・・ cluster schedulerに移行したい・・・ cron cluster とか lsyncd 使うしイヤだ！！ そこでじゃじゃ〜〜ん！ Dkron - Distributed, faul…"
---

# dkron - cronやめたい

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[yusukeh](/authors/yusukeh/)
-   公開日：2016年9月14日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#Consul](/tags/consul/)[#dkron](/tags/dkron/)

![dkron - cronやめたい](/_astro/ogp.BR7em1Ed_Z2a1bHb.webp)

こんにちは！grasys長谷川です！

cron・・・めっちゃ困るcron・・・  
古いぜcron・・・もういやだcron・・・  
cluster schedulerに移行したい・・・  
**cron cluster**とか**lsyncd**使うしイヤだ！！

そこでじゃじゃ〜〜ん！

[![dkron - cronやめたいのロゴ](/_astro/01-dkron-logo-blue-7fe5a478.Di_sRbzO_Z1LjdL7.webp)](https://dkron.io)

#### Dkron - Distributed, fault tolerant job scheduling system

grasysで社内的な要件だけど実際に業務上で導入してみました！！

**こういうの楽しい！  
やばい楽しい！！！**

ある程度の期間の安定運用が可能になってきたら実際にお客様にもオススメしようかと考えてます。

もうconfig.jsonの自動生成やらjobとかの自動登録とかの仕組みも作ってある！  
こういうの土日とか空いてる時間使ってコソコソやってます。ｗ

* * *

## なぜやってみようと思ったか

1.  **consulをbackend**にできる
2.  **API**化されてる
3.  success: **true/false**で確認できる
4.  jobのrole指定の中で`[role name]:[num]`で同じroleにいるインスタンス内での実行数を制御できる **同じ構成のclusterの中で特定jobを１台だけで実行するとかね**

> consulだけじゃなくてetcd, ZooKeeperでもできるみたい

ここでは構成の仕方というより紹介って感じの内容にします。

## dkron dashboardの画面

dashboardはこんな感じ

![dkron dashboardの画面](/_astro/02-dkron_dashboard-1024x886-fdd756f8.xZD9FLsk_Z1vNG8m.webp)

failedでてるのはたぶんjob消したり登録し直したりしてて出てますｗ

* * *

## dkron jobs

job一覧はこんな感じ

![dkron jobsの画面](/_astro/03-dkron_jobs-1024x886-59bfd8d7.VYV4TcDK_Zwsw3M.webp)

* * *

## dkron show job

jobの内容はこんな感じ

登録した時のJSONの出力にstatus的なものが加わったのが返ってくる

![dkron show jobの画面](/_astro/04-dkron_show_job-1024x885-8c098c83.BEj71GCK_Z1fJ74o.webp)

`success_count`, `error_count`, `last_success`, `last_error`がなんか変だけどｗ 今のはv0.9.0で  
ひとつ前のversionだと正常だった・・・ｗ

* * *

## dkron executions

実行結果一覧はこんな感じ

![dkron executionsの画面](/_astro/05-dkron_executions-1024x885-5703bf9f.CU1pxxil_ZYa8Mt.webp)

* * *

## consul kv dkron

consul kvに保存されてるdkronのデータはこんな感じ

![Consul KVに保存されたDkronのデータ](/_astro/06-dkron_consul_kv-1024x485-6bf537bc.BnFkyTTj_1XEj08.webp)

* * *

## job

jsonで\*\*/jobs\*\*にPOSTしてあげると登録できます。  
DELETEで投げると削除になる

Plain textcontent\_copy

```
{
  "command": "xxxxx xxxxx xxxxxx",
  "disabled": 0,
  "name": "xxxxxxxxxx",
  "owner": "xxxxx",
  "owner_email": "xxxxx@grasys.io",
  "run_as_user": "xxxxx",
  "shell": 1,
  "schedule": "1 */10 * * * *",
  "tags": {
    "role": "bt:1"
  },
}
```

## クラスタ内で実行する数を制御

`[role name]:[num]`でクラスタ内で実行してくれます。

Plain textcontent\_copy

```
    "tags": {
      "role": "bt:1"
    },
```

こういうとこが便利！

あとは普通に**実行結果とかも取れるんで便利**だと思う

### schedule

[Guides - Dkron](http://dkron.io/docs/guides/)

### CRON Expression Format

[CRON Expression Format Guides - Dkron](http://dkron.io/docs/guides/#cron-expression-format)

| Field name | Mandatory? | Allowed values | Allowed special characters |
| --- | --- | --- | --- |
| Seconds | Yes | 0-59 | * / , - |
| Minutes | Yes | 0-59 | * / , - |
| Hours | Yes | 0-23 | * / , - |
| Day of month | Yes | 1-31 | * / , - ? |
| Month | Yes | 1-12 or JAN-DEC | * / , - |
| Day of week | Yes | 0-6 or SUN-SAT | * / , - ? |

> 上記Documentの引用

cronみたいに

Plain textcontent\_copy

```
"schedule": "1 */10 * * * *"
```

### Predefined schedules

| Entry | Description | Equivalent To |
| --- | --- | --- |
| @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 * |
| @monthly | Run once a month, midnight, first of month | 0 0 0 1 * * |
| @weekly | Run once a week, midnight on Sunday | 0 0 0 * * 0 |
| @daily (or @midnight) | Run once a day, midnight | 0 0 0 * * * |
| @hourly | Run once an hour, beginning of hour | 0 0 * * * * |
| @minutely | Run once a minute, beginning of minute | 0 * * * * * |

毎時動かしたい時

Plain textcontent\_copy

```
"schedule": "@hourly"
```

### Intervals

Plain textcontent\_copy

```
"schedule": "@every <duration>"
```

**便利！！！**

## API

[API - Dkron](http://dkron.io/docs/api/)

詳細は上記URLにあります。

叩くとJSONで返ってきます。  
一通り運用するに必要そうなものが準備されてる感じかな？

Plain textcontent\_copy

```
curl -s localhost:8947/v1/jobs | jq .
[
  {
    "name": "xxxxx-xxxxxxxxxx",
    "schedule": "1 */10 * * * *",
    "shell": true,
    "command": "xxxx xxxx xxxxxxxxx",
    "owner": "xxxxx",
    "owner_email": "xxxxxx@grasys.io",
    "success_count": 0,
    "error_count": 0,
    "last_success": "0001-01-01T00:00:00Z",
    "last_error": "0001-01-01T00:00:00Z",
    "disabled": false,
    "tags": {
      "role": "bt:1"
    },
    "retries": 0,
    "dependent_jobs": null,
    "parent_job": ""
  }
]
```

shellはbashとかの環境変数が必要なときにtrueしてもらえればと

> 実際curlする際にはdkronのconfig.jsonのportでやる必要があります。

## 実際使うには・・・

もう少し待った方がいいかな  
でも動きは良いと思うから利用価値はありそう。

画面付いてた方が良いならぶっちゃけRundeckとかの方がいいかも

ある程度決め打ちで自動構成含めた視野があるならこっちの方がシステマチックな感じになる気がする。

Blogだから一応いろいろ情報のせてみたけど  
ぶっちゃけ触ってみるといろいろわかると思います。。。。

grasysはconsulが中心になったアーキテクチャなので  
簡単なんだけど・・・  
スケジューラでconsulやらetcdやらZooKeeperとかを別途Setupするのはちょっと重いよなぁ・・・

## この記事を書いた人

[![](/_astro/yusukeh.BvUd2yKE_1mQ6Fp.webp)](/authors/yusukeh/)

### [yusukeh](/authors/yusukeh/)

Fly high. Think deep. Move fast. Go far.🦅

-   [X](https://x.com/yusukeh "X")
-   [GitHub](https://github.com/yusukeh "GitHub")
-   [Medium](https://medium.com/@yusukeh "Medium")
-   [Facebook](https://www.facebook.com/yusuke.exzm "Facebook")
-   [Instagram](https://www.instagram.com/yusukehasegawa/ "Instagram")
-   [LinkedIn](https://www.linkedin.com/in/hasegawa-yusuke-091b4164/ "LinkedIn")

[プロフィールと記事一覧](/authors/yusukeh/)