2月19日追記
repeat-exec.ahkを大幅に改変した。
fenrirScan*1を一定時間毎に起動したいと考えたところ、windowsで動作するcrontab - Wikipediaのようなものが見当たらなかったので*2、単機能なAutoHotkey_L用スクリプトを作成した*3。
exe化したrepeat-exec.exeをfenrirScan.exeのフォルダに置き、以下のようにショートカットかコマンドプロンプトで実行すること。こうすると、60秒毎に fenrirScan.exe を実行する。
- repeat-exec.exe --program fenrirScan.exe --time 60
60分毎に実行するならこう記述する。
- repeat-exec.exe --program fenrirScan.exe --time 3600
60分毎に実行するときには --time 3600 を省略できる。
- repeat-exec.exe --program fenrirScan.exe
初回起動時に限ってプログラムを実行しないようにするには、--not_executing を記述する。
- repeat-exec.exe --program fenrirScan.exe --not_executing
なお、--program のあとに記述するのは、実のところ、実行ファイルでなくともよい。テキストファイルを記述すると、そのテキストファイルを一定時間毎に開く。
ソースは以下の通りである。指定したプログラムに引数を渡せるようにするなど、このスクリプトを改変する余地は多々あるので、自由に編集してほしい。
;; repeat-exec.exe ;; --program program_name で繰り返し起動するプログラム名を指定する ;; --time 60 で 60秒間隔でプログラムを起動 ;; --time 600 で 600秒(10分)間隔でプログラムを起動 ;; --not_executing があるときには、初回のプログラム起動をスキップする program_name := False ;; 初回はプログラムを起動しないか not_executing_for_the_first_time := False sec := 1000 min := 60 * sec hour := 60 * min ;; コマンドオプションが指定されていなければ、1時間毎にプログラムを起動する sleep_time := hour ;; コマンドオプションの取得 arg1=%1% arg2=%2% arg3=%3% arg4=%4% arg5=%5% arg_list := [arg1,arg2,arg3,arg4,arg5] for i,v in arg_list { if (v = "--time"){ sleep_time := arg_list[i+1] * sec } if (v = "--not_executing") { ;; --not_executing が指定されているときには、初回のプログラム起動をスキップする sleep_time := True } if (v = "--program") { program_name := arg_list[i+1] } } try { check_file_exist(program_name) } catch e { msgbox,% "Error: " program_name " is not found." ExitApp } loop { tmptime := A_TickCount if not ((A_Index = 1) and (not_executing_for_the_first_time)){ run,% program_name } elapsed_time := A_TickCount - tmptime sleep, % sleep_time - elapsed_time } return check_file_exist(file) { if (FileExist(file) = ""){ throw Exception("Fail", -1) } }
*2:wikiではタスクスケジューラの使用を勧めている。
*3:try/catch を使用しているので、元祖 AutoHotkey では動作しない。