# Restart a program when it terminates # Date : 22/01/2006 # Name : kautorestart - Auto-restart (for windows game servers) # Author : Kabeldag # Purpose : Automatically restarts windows game servers when they terminate/crash/end etc .. # Not the double-back slashes and the date, heh ;) use strict; use Win32::Process; use Win32; print "\r\n"; print "Started Auto-Restart for Windows Game-Servers ...\n"; print "Created by kabeldag on 22-01-2006\n\n"; my ($server_exe_file,$server_dir,$params)=@ARGV or do_error("No arguments specified\n"); if(!$server_exe_file) { do_error("Server executable not specified"); }elsif(!$server_dir) { do_error("Server directory not specified"); }elsif(!(-e $server_dir)) { do_error("Directory of : $server_dir not found"); }elsif(!(-e "$server_dir\\$server_exe_file")) { do_error("File not found : $server_dir\\$server_exe_file"); } sub do_error() { my $err_msg=$_[0]; print "$err_msg\n"; print "Syntax: $0 server path-of-server params-for-server\n"; print "\nExample:\n\n$0 \"moh_spearhead_server.exe\" \"c:\\moh-server\\\" \"+exec server.cfg\"\n"; exit(); } sub ErrorReport{ print Win32::FormatMessage(Win32::GetLastError()); } sub print_line($) { my $line_in=$_[0]; my $time=localtime(); print "->$time -> $line_in\n"; } sub start_process() { my $processObj; print "============- [PRESS CTRL+C TO TERMINATE] -=================\n"; print_line("Starting new process ..."); Win32::Process::Create($processObj, "$server_dir\\$server_exe_file", "$server_exe_file $params", 0, NORMAL_PRIORITY_CLASS, "$server_dir")||die ErrorReport(); monitor_process($processObj); } sub monitor_process() { my $processObj=$_[0]; my $pid=$processObj->GetProcessID(); print_line("Process id is $pid"); print_line("Waiting on process $pid"); $processObj->Wait(INFINITE); #$processObj->Kill(1); print_line("Process $pid ended"); start_process(); } start_process();