0
1

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 1 year has passed since last update.

LinuxでCPUのリソース制限をつけてプロセスを起動する

Posted at

はじめに

以前PHPを(動的に)20倍遅くする方法という記事を書きました

この時は、サービスとしてバックグランドで実行中のプロセスに対して、動的にCPUクォータの設定を変更したのですが、新規にプロセスを起動する場合にどのようにCPUクォータを設定すればいいか?と思い調べました

systemd-run を使うことで簡単に実現に実現できました

実行手順

  • CPUを最大10%に制限して<command>を実行する
    • --scope:一時的な スコープ ユニットを作成して実行(未指定の場合、サービスユニットとしてバックグラウンド実行される)
    • --uid:未指定の場合rootとして実行されるため、現在のユーザー(whoami)を指定
    • -p:cgroupsのプロパティーを指定(CPUQuota)
sudo systemd-run --scope --uid=`whoami` -p CPUQuota=10% <command>
Running scope as unit: run-r14b02a6f181f44bb9e5109f5fbdbbe31.scope

例1:yesコマンド(標準出力にyを出力するだけ)を、CPU負荷10%で実行(&をつけてバックグラウンド実行)

sudo systemd-run --scope --uid=`whoami` -p CPUQuota=10% yes > /dev/null &
[3] 1674
Running scope as unit: run-r14b02a6f181f44bb9e5109f5fbdbbe31.scope

例2:一時的につけられたユニット名を利用して、CPUクォータを変更する

sudo systemctl set-property --runtime run-r14b02a6f181f44bb9e5109f5fbdbbe31.scope CPUQuota=50%

例3:終了(jobsで番号を調べてから終了)

$ jobs
[1]   実行中               sudo systemd-run --scope --uid=`whoami` -p CPUQuota=10% yes > /dev/null &  (wd: ~)
[2]-  実行中               sudo systemd-run --scope --uid=`whoami` -p CPUQuota=10% yes > /dev/null &

kill %1 %2 

systemd-run

  • 特定のコマンドを、systemd管理下で実行するためのツール
  • プロセスに対して一時的な cgroupを作成したり、定期的に実行することができる
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?