-
Notifications
You must be signed in to change notification settings - Fork 30
/
restarter.pl
executable file
·93 lines (72 loc) · 2.29 KB
/
restarter.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl
#
# Copyright 2013 by Denis Erygin,
#
use warnings;
use strict;
use constant PORT => 2302; # Change it with epoch.sh
use constant PATH => $ENV{'PWD'}.'/'; # Set your epoch server dir
use constant PIDFILE => PATH.PORT.'.pid';
use constant CACHE_DIR => PATH.'cache/players';
unless (-f PATH.'epoch') {
print STDERR "Can't found server binary!\n";
exit;
}
set_time ();
logrotate ();
if (-f PIDFILE) {
open (IN, '<'.PIDFILE) or die "Can't open: $!";
my $pid = int(<IN>);
close (IN);
my $res = `kill -TERM $pid 2>&1`;
print STDERR $res,"\n" if $res;
unlink (PIDFILE) if (-f PIDFILE);
backup_cache();
}
print STDERR "Restart Dayz Epoch server...\n";
chdir (PATH);
my $cmd = '/usr/bin/screen -h 20000 -fa -d -m -S epoch '.PATH.'epoch.sh';
my $res = `$cmd`;
print STDERR $res,"\n" if $res;
exit;
#-----------------------------------------------------------------------------------------------
sub set_time {
my ($s, $m, $h, $day, $mon, $y) = localtime(time() - 3*3600);
$y += 1900;
$mon++;
# Uncomment to disabe night
#($h, $m) = (17, 0) if ($h > 17 || ($h >= 0 && $h < 4));
my $file = PATH.'cache/set_time.sqf';
open (IN, ">$file") or die "Can't find $file";
# ["PASS", [year, month, day, hour, minute]]
print IN '["PASS",[2012,6,6,'.$h.','.$m.']]'; # with full moon
close (IN);
}
sub logrotate {
my $log = PATH.'dump.log';
if (-f $log) {
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat($log);
if ($size && $size >= 100000000) {
print STDERR "logrotate $size\n";
my $nlog = $log.'.'.time();
my $res = `cp $log $nlog 2>&1`;
print STDERR $res,"\n" if $res;
$res = `echo '' > $log 2>&1`;
print STDERR $res,"\n" if $res;
}
}
}
sub backup_cache {
return unless (-d CACHE_DIR);
opendir (DIR, CACHE_DIR) or die $!;
while (my $file = readdir (DIR)) {
next unless ($file =~ m/^\d+$/ && $file ne '1');
my $dir = CACHE_DIR.'/'.$file;
my $backup = CACHE_DIR.'/1';
next unless (-d $dir);
my $res = `mv -f $dir $backup 2>&1`;
print STDERR $res,"\n" if $res;
}
closedir (DIR);
}