Skip to content

Instantly share code, notes, and snippets.

@bczhc
Created May 19, 2022 15:42
Show Gist options
  • Select an option

  • Save bczhc/ede10e8e6dcd166fe466d89392ca1d91 to your computer and use it in GitHub Desktop.

Select an option

Save bczhc/ede10e8e6dcd166fe466d89392ca1d91 to your computer and use it in GitHub Desktop.
Minecraft服务器定时重启脚本
#!/bin/env ruby
self_name = File.basename($0)
argv = ARGV
if argv.size < 2
puts "Usage: #{self_name} <screen-name> <command> [screen-select]"
exit 1
end
$screen_name = argv[0]
command = argv[1]
$screen_select = argv[2]
def send_command(cmd)
select_arg = $screen_select == nil ? "" : " -p '#{$screen_select}'"
cmd = %{screen -xr #{$screen_name}#{select_arg} -X stuff '#{cmd}\n'}
puts "Run: #{cmd}"
system cmd
end
loop do
# restart the server with interval of 40 minutes
sleep 40 * 60
send_command "say server will be restarted in 5 minutes"
sleep 5 * 60
send_command "say server will be restarted in 1 minute"
sleep 1 * 60
send_command "say restarting..."
# stop and then relaunch the command
send_command "save-all"
send_command "stop"
# wait for the server to stop
sleep 1 * 60
# restart
send_command command
# estimate the time to restart the server (10 minutes), do a time skip, and then loop again
# to schedule the next restart
sleep 10 * 60
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment