3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JobSchedulerのREST-APIを使ってみる - Shellで実装編-

Posted at

JobSchedulerのREST-APIをShellから使ってみるサンプルです。

REST-API、jqを導入してShellで実装すると更に楽々なんです。

要件的にJava実装縛りなどがなければ、実は基盤ツールとしてはこちらの方がおすすめかもです(汗)

【jqの導入】

sudo curl -o /usr/bin/jq -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 && sudo chmod +x /usr/bin/jq

【Shell実装のサンプル】

#!/bin/bash

#RESPONSE取得(JSON形式で返却されます)

JSON_RESPONSE=$(curl -X POST -H "Authorization: Basic cm9vdDpyb290" -H "Content-Type: application/json" -H "Cache-Control: no-cache" http://localhost:4446/joc/api/security/login | jq ".accessToken")

#返却値のJSONからACCESS_TOKENを取得

ACCESS_TOKEN=$(echo $JSON_RESPONSE | jq "." | sed "s/\"//g")
echo $ACCESS_TOKEN

#ヘッダフィールドに$ACCESS_TOKEN変数の値をセットしてコマンドを実行

RET=$(curl -X POST -H "access_token: $ACCESS_TOKEN" -H "Content-Type: application/xml" -H "Cache-Control: no-cache" http://localhost:4446/joc/api/jobscheduler/commands -d '<jobscheduler_commands jobschedulerId="scheduler"><start_job job="sample/job1" at="now" /></jobscheduler_commands>' | jq ".")

#実行結果でエラーが発生している場合には以下のように結果のレスポンスの「error」にて取得可能

ERROR_MESSAGE=$(echo $RET | jq ".error")
echo $ERROR_MESSAGE

#適宜エラーハンドルを記述!
#;;

ああ楽ちん、です

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?