LoginSignup
1
1

More than 3 years have passed since last update.

dotnet core(C#言語)で、コンソールの多重起動制御(Mutexを使う方法)をLinuxでも行う

Last updated at Posted at 2020-10-09

Mutexクラスを使って多重起動制御を行う

Windowsだと、普通にnew Mutex(false, "hogehoge");とすれば、多重制御処理を書けてしまいますが、Linuxだと発動したり発動しなかったり。

多重制御が効く

/usr/local/bin/Hoge/Hoge &
/usr/local/bin/Hoge/Hoge &

多重制御が効かない

0 0 * * * root /usr/local/bin/Hoge/Hoge
5 0 * * * root /usr/local/bin/Hoge/Hoge

Mutexクラスで多重起動制御できるようにする

Global\ 文字列を付けるだけみたいです。

var mutex = new System.Threading.Mutex(false, @"Global\\Hoge");
try {
    handle = mutex.WaitOne(0, false);
}
catch( System.Threading.AbandonedMutexException) {
    handle = true
}

if( !handle ) {
    // 多重起動
    return;
}

cronでもちゃんと制御してくれました!!

  • dotnet core 3.1 / centos7, centos8 で確認
1
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
1
1