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 3 years have passed since last update.

schtasks.exe – コマンドを時刻指定して実行する

Posted at

1日の業務の中で決まった時刻に特定のコマンドを実行したい場合には、タスクスケジューラで登録すると便利です。

手順と概略

Windowsではタスクスケジューラとして schtasks.exe が用意されているので、

  1. 実行したいコマンドをシェルスクリプトとして作成
  2. 1.のファイルを schtasks.exe を実行する schtasks コマンドにてスケジュール登録

という手順で設定します。

1. シェルスクリプト

まずは実行したいコマンドをシェルファイルとして作成しておきます。

例えば、任意のURLを開くコマンドを実行したいとして下記のように組んで保存します。

# ! /bin/sh
start https://google.com

便宜上、 daily.shというファイル名でホームディレクトリ( C:\Users\ユーザ名)にでも保存しておきます。

2. schtasks コマンド

次にpowershellかコマンドプロンプトで、上記の daily.sh を指定してスケジュール登録します。

schtasks /create /tn "MyTask" /tr c:\Users\ユーザ名\daily.sh /sc weekly /d mon,tue,wed,thu,fri /st 18:00

設定の解説

オプション 説明 この場合の設定値
/tn 任意のタスク名 "MyTask"
/tr 実行したいタスクのファイルパス c:\Users\ユーザ名\daily.sh
/sc スケジュールのタイプ 週次
/d 実行する日のタイプ (月)(火)(水)(木)(金)
/st 実行する時刻 18:00

上記を含む schtasks コマンドのオプションについては Microsoftの公式ドキュメントに詳細が書かれています。


この一連のコマンドは 毎平日に定時になったら実行することを忘れたくないコマンド を登録しておくイメージです。

こういう些細な作業こそ、ガシガシ自動化しちゃいましょう!

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?