一定時間たったら処理を終了させるコマンドが便利【timeout/gtimeout】

コンピュータ関連
スポンサーリンク

以前ネット配信の番組を自動的に録画する仕組みをつくりました。

基本的にはそれで問題ないのですが、
放送(内容)は終了してもストリーミング(配信自体)はその後もしばらく続くような場合、
当然ながら配信が完全に終了するまで録画し続けてしまいます。

つまりオフラインになるまで余計に1、2時間録画し続けてしまうのです。

これを止めるため
「一定時間経過したら録画を止めるコマンド」
が必要となりました。

timeout というコマンドがLinuxで使えるようですが、
MacOSにはなかったので Homebrew から Coreutils を入れて使えるようにしました。

そんな備忘録。

スポンサーリンク

時間になったら処理(録画)を開始し、一定時間で終了する

コマンドの利用については、別に録画に限った話ではないので、
関係なければ要らないところは読み飛ばしていただいて構いません。

スケジュール録画についてはちょっと試行錯誤したのでいくつかの記事に分かれてます。
この辺を見たら多分わかります。

ストリーム配信を録画する為に定期的にジョブを実行するcrontabを使う
以前カレンダーを使ってネット配信番組の動画を予約録画する記事を書きました。 それにちょっとした問題が発生したので、 今はcrontabを使って起動するようにしています。 要するにカレンダーで起動していた録画実行スクリプトを、 crontab...

さて、とにかく処理を開始した後、一定時間経過したら終了するようにしたいわけです。

Homebrewからcoreutilsをインストール

そのためのコマンド群をインストールしました。
Homebrewを使いました。

% brew install coreutils
...
==> Pouring coreutils--9.5.monterey.bottle.tar.gz
==> Caveats
Commands also provided by macOS and the commands dir, dircolors, vdir have been installed with the prefix "g".
If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH with:
  PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
==> Summary
🍺  /usr/local/Cellar/coreutils/9.5: 481 files, 14.2MB

無事インストールが完了し、「gtimeout」が使えるようになりました。(エイリアスが作成され「timeout」でも通ります)

% timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Mandatory arguments to long options are mandatory for short options too.
      --preserve-status
                 exit with the same status as COMMAND, even when the
                   command times out
      --foreground
                 when not running timeout directly from a shell prompt,
                   allow COMMAND to read from the TTY and get TTY signals;
                   in this mode, children of COMMAND will not be timed out
  -k, --kill-after=DURATION
                 also send a KILL signal if COMMAND is still running
                   this long after the initial signal was sent
  -s, --signal=SIGNAL
                 specify the signal to be sent on timeout;
                   SIGNAL may be a name like 'HUP' or a number;
                   see 'kill -l' for a list of signals
  -v, --verbose  diagnose to stderr any signal sent upon timeout
      --help        display this help and exit
      --version     output version information and exit

DURATION is a floating point number with an optional suffix:
's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.
A duration of 0 disables the associated timeout.

Upon timeout, send the TERM signal to COMMAND, if no other SIGNAL specified.
The TERM signal kills any process that does not block or catch that signal.
It may be necessary to use the KILL signal, since this signal can't be caught.

Exit status:
  124  if COMMAND times out, and --preserve-status is not specified
  125  if the timeout command itself fails
  126  if COMMAND is found but cannot be invoked
  127  if COMMAND cannot be found
  137  if COMMAND (or timeout itself) is sent the KILL (9) signal (128+9)
  -    the exit status of COMMAND otherwise

GNU coreutils online help: https://www.gnu.org/software/coreutils/
Report any translation bugs to https://translationproject.org/team/
Full documentation https://www.gnu.org/software/coreutils/timeout
or available locally via: info '(coreutils) timeout invocation'

gtimeout(timeout)の使い方

使い方は単純、終了させたい時間を指定すればOK
秒:10s
分:20m
時:1h など

#サンプル 1時間後に録画終了
% timeout 1h yt-dlp 'https://www.配信サーバー/URI' -o ~/Desktop/output.mp4 

これで1時間後に録画を終了します。

実際には途中でスリープに入らないよう caffeinate も使っています。
こちらはMacで元々使えるもので、スリープを抑制するコマンドです。
録画途中でスリープに入られると厄介なので。

ま、その辺は適宜対応してください。では。

コメント

タイトルとURLをコピーしました