備忘録

備忘録

systemdをcron代替として利用する方法

Ⅰ. はじめに

タイトルの通り「systemdをcron代替として利用する方法」です。
例として「10秒ごとにecho helloを実行する方法」をこの記事で紹介します。

一時的なスケジュールを簡単に作成する方法はこちら

Ⅱ. やり方

1. サービスユニットファイルを作成する

/etc/systemd/system/Hello.service

[Unit]
Description=Hello service

[Service]
Type=oneshot
ExecStart=echo hello
2. タイマーユニットファイルを作成する

/etc/systemd/system/Hello.timer

[Unit]
Description=Hello timer

[Timer]
# OS起動後10分経過後に実行する
OnBootSec=10min

# OS起動後1時間15分経過後に実行する
# OnBootSec=1hour 15min

# 10秒ごとに実行
OnUnitActiveSec=10s

# タイマー精度
AccuracySec=100ms

# OSが停止等で前回未実行の場合直ちに実行する
# Persistent=true
3. 起動する
systemctl start Hello.timer
4. 状態確認
systemctl list-timers

f:id:kagasu:20210629174213p:plain

結果

10秒ごとに実行されている

$ journalctl -fu Hello
Jun 29 17:42:13 localhost echo[4056]: hello
Jun 29 17:42:13 localhost systemd[1]: Hello.service: Succeeded.
Jun 29 17:42:13 localhost systemd[1]: Finished Hello service.
Jun 29 17:42:23 localhost systemd[1]: Starting Hello service...
Jun 29 17:42:23 localhost echo[4066]: hello
Jun 29 17:42:23 localhost systemd[1]: Hello.service: Succeeded.
Jun 29 17:42:23 localhost systemd[1]: Finished Hello service.
Jun 29 17:42:33 localhost systemd[1]: Starting Hello service...
Jun 29 17:42:33 localhost echo[4080]: hello
Jun 29 17:42:33 localhost systemd[1]: Hello.service: Succeeded.
Jun 29 17:42:33 localhost systemd[1]: Finished Hello service.

node-fetchでCookieを設定する方法

Ⅰ. はじめに

タイトルの通り「node-fetchでCookieを設定する方法」です。

Ⅱ. やり方

1. サンプルプログラムを書く
const fetch = require('node-fetch');

(async () => {
  const url = 'https://google.com'

  // 初回リクエスト。Set-Cookieレスポンスヘッダを読み取る。
  const res = await fetch(url)
  const cookies = res.headers.raw()['set-cookie']
  const cookie = cookies.map(x => x.split(';')[0]).join('; ')

  // リクエストヘッダにCookieを設定してリクエストする
  await fetch(url, {
    headers: {
      'Cookie': cookie
    }
  })
})()

実行結果

省略

C#でWindows 10のOCRを利用する方法

Ⅰ. はじめに

タイトルの通り「C#Windows 10のOCRを利用する方法」です。

Ⅱ. 環境

Ⅱ. やり方

1. csprojを編集してTargetFrameworkを変更する
<TargetFramework>net5.0-windows10.0.18362.0</TargetFramework>
2. サンプルプログラムを書く

123.png
f:id:kagasu:20210614105758p:plain

Program.cs

using System;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
using Windows.Storage;
using Windows.Storage.Streams;

namespace OcrTest
{
  class Program
  {
    static async Task Main(string[] args)
    {
      var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();

      // ローカルファイルの場合
      var storageFile = await StorageFile.GetFileFromPathAsync("C:\\123.png");
      using var iRandomAccessStream = await RandomAccessStreamReference.CreateFromFile(storageFile).OpenReadAsync();

      // URLの場合
      // var uri = new Uri("https://i.imgur.com/LkJ8ZEJ.png");
      // using var iRandomAccessStream = await RandomAccessStreamReference.CreateFromUri(uri).OpenReadAsync();

      var bitmapDecoder = await BitmapDecoder.CreateAsync(iRandomAccessStream);
      using var softwareBitmap = await bitmapDecoder.GetSoftwareBitmapAsync();

      var result = await ocrEngine.RecognizeAsync(softwareBitmap);
      Console.WriteLine(result.Text);
    }
  }
}

実行結果

123

類似記事

C#でTesseractを利用する方法

Ⅰ. はじめに

タイトルの通り「C#でTesseractを利用する方法」です。

Ⅱ. やり方

1. 必要なパッケージをNuGetからインストールする
dotnet add package Tesseract --version 5.2.0
2. 学習済みデータを任意のディレクトリに保存する

例. C:\Tesseract\eng_fast.traineddata

3. サンプルプログラムを書く

123.png

Program.cs

using Tesseract;

using var engine = new TesseractEngine("C:\\Tesseract", "eng_fast");
engine.SetVariable("tessedit_char_whitelist", "0123456789");
engine.SetDebugVariable("debug_file", "null");

var page = engine.Process(Pix.LoadFromFile("C:\\123.png"));
Console.WriteLine(page.GetText());

実行結果

123

類似記事

node-fetchでproxyを設定する方法

Ⅰ. はじめに

タイトルの通り「node-fetchでproxyを設定する方法」です。

Ⅱ. やり方

1. 必要なパッケージをインストールする
npm install node-fetch
npm install http-proxy-agent
npm install https-proxy-agent
2. サンプルプログラムを書く

index.js

const fetch = require('node-fetch');
// const HttpProxyAgent = require('http-proxy-agent');
const HttpsProxyAgent = require('https-proxy-agent');

(async () => {
  // process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

  const proxyAgent = new HttpsProxyAgent('http://10.0.0.2:3128')
  // const proxyAgent = new HttpsProxyAgent('http://username:password@10.0.0.2:3128')
  const response = await fetch('https://api.ipify.org/?format=json', { agent: proxyAgent })
  const json = await response.json()
  console.log(json)
})()

実行結果

$ node index.js
{ ip: '107.x.x.x' }