タイムアウトによりbashが終了した場合のコマンド実行

タイムアウトによりbashが終了した場合のコマンド実行

私は使用すべき解決策を知っています。

trap your_command EXIT

しかし、私が望むのは、TMOUT変数の設定タイムアウトのためにbashが終了した場合にのみコマンドを実行することです。脱出口はありますか?

私の環境:RedHat 7.7

ベストアンサー1

私が見ることができる唯一のオプションは、~/.bash_logoutファイルにコマンドを置くことです。現在のシェルがログインシェルで、現在のシェルがサブシェルでない場合は、そのファイルを実行します。

内部に読み取りコマンドが評価されるbashのソースコード、値の警告ハンドラを設定しますTMOUT

  if (interactive)
    {
      tmout_var = find_variable ("TMOUT");

      if (tmout_var && var_isset (tmout_var))
      {
        tmout_len = atoi (value_cell (tmout_var));
        if (tmout_len > 0)
        {
          old_alrm = set_signal_handler (SIGALRM, alrm_catcher);
          alarm (tmout_len);
        }
      }
    }

それアラームハンドラ次のようになります。

static sighandler
alrm_catcher(i)
     int i;
{
  printf (_("\007timed out waiting for input: auto-logout\n"));
  fflush (stdout);
  bash_logout ();   /* run ~/.bash_logout if this is a login shell */
  jump_to_top_level (EXITPROG);
  SIGRETURN (0);
}

これbash_logout関数は次のように定義されます。:

void
bash_logout ()
{
  /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
  if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
    {
      maybe_execute_file ("~/.bash_logout", 1);
#ifdef SYS_BASH_LOGOUT
      maybe_execute_file (SYS_BASH_LOGOUT, 1);
#endif
    }
}

おすすめ記事