This document contains the content of /resources/other/mc-server-setup-arch.sh. Download the file by setting the ?act=download parameter, or access the raw file at either srcs.cc or src.cerium.cc.
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 #!/bin/bash p="#" a1="=>" a2=" ->" set -euo pipefail # Introduction. clear -x echo "This script is able to: create a user, install Java, download and run an" echo "installer, update an existing server, create a launch script, create a systemd" echo "service unit, and create a shortcut command to run commands directly on the" echo "Minecraft console and view the output." echo "" echo "This script should be run as root on an unused installation of Arch Linux." echo "Before starting, have a directory prepared to store game data." echo "" echo "If you plan to manually install a Java version or use your own server installer," echo "make sure to prepare them before starting this script. In the case of a server" echo "installer, just make sure to know the file path." echo "" echo "It is safe to abort/stop the script via Ctrl+C at any input prompt, and you can" echo "get back to any point just by skipping most steps and redoing a couple." echo "" echo "NOTICE: The Minecraft Fabric installer supports versions from 1.14 to latest." echo " For other Minecraft versions, download the desired server installer file" echo " manually before running this script." echo "NOTICE: The available options for Java are: 8, 11, 17, 21, and 25, or anything" echo " else available from the Arch repositories." echo " For other Java versions, install it manually. The AUR has many." while true; do echo "" read -rp "$p Are you ready to proceed? [Y/n] " proceed case "$proceed" in [Yy]|"" ) break;; [Nn] ) echo "$a1 Exiting."; exit;; * ) echo "$a1 Invalid response.";; esac done # Compatibility tests. echo "" echo "$a1 Running compatibility tests..." test1="" test2="" test3="" test4="" test5="" test6="" tests_failed="" echo -n "$a2 Checking curl... " if curl --version &> /dev/null; then echo "PASS"; else test1="fail"; echo "FAIL"; fi echo -n "$a2 Checking for systemd... " if [ -d "/run/systemd/system" ]; then echo "PASS"; else test2="fail"; echo "FAIL"; fi echo -n "$a2 Checking root privileges... " if [ "$EUID" = "0" ]; then echo "PASS"; else test3="fail"; echo "FAIL"; fi echo -n "$a2 Checking /etc/systemd/system/ permissions... " if touch "/etc/systemd/system/permission.test" &> /dev/null && rm "/etc/systemd/system/permission.test" &> /dev/null; then echo "PASS"; else test4="fail"; echo "FAIL"; fi echo -n "$a2 Checking connection... " if [ "$(curl -o /dev/null -ILsw "%{response_code}" https://archlinux.org/)" -eq 200 ]; then echo "PASS"; else test5="fail"; echo "FAIL"; fi echo -n "$a2 Checking fabricmc.net connection... " if [ "$(curl -o /dev/null -ILsw "%{response_code}" https://maven.fabricmc.net/)" -eq 200 ]; then echo "PASS"; else test6="fail"; echo "FAIL"; fi if [ "$test1" = "fail" ]; then echo "WARNING: curl command error, please skip the Fabric installer." tests_failed="true"; fi if [ "$test2" = "fail" ]; then echo "WARNING: this system is not using systemd, please skip creating unit files." tests_failed="true"; fi if [ "$test3" = "fail" ]; then echo "WARNING: root privileges are not detected, please run again as root." tests_failed="true"; fi if [ "$test4" = "fail" ]; then echo "WARNING: cannot write to systemd directory, please skip creating unit files." tests_failed="true"; fi if [ "$test5" = "fail" ]; then echo "WARNING: cannot connect to archlinux.org, downloads will likely fail. If you can" echo " verify that you can install packages, then it's safe to proceed." tests_failed="true"; fi if [ "$test6" = "fail" ]; then echo "WARNING: cannot connect to maven.fabricmc.net, please skip the Fabric installer." tests_failed="true"; fi if [ "$tests_failed" = "true" ]; then while true; do echo "" read -rp "$p At least one test failed, are you sure you want to proceed? [y/N] " skip_warn case "$skip_warn" in [Yy] ) echo "$a1 Ignoring warnings..."; break;; [Nn]|"" ) echo "$a1 Exiting."; exit;; * ) echo "$a1 Invalid response.";; esac done else echo "" echo "$a1 All tests completed successfully." fi # Create/select a user. while true; do echo "" read -rp "$p Do you want to create a new user? [y/N] " create_user case "$create_user" in [Yy] ) echo "$a1 Creating a user..." echo "" read -rp "$p Enter the user name: " user echo "$a2 Checking if user exists." if id "$user" &> /dev/null; then echo "$a2 User '$user' already exists." continue fi echo "" echo "The home directory must be specified as an absolute file path." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the home directory path (ENTER for '/home/$user'): " home_dir_pattern if [ ! "$home_dir_pattern" ]; then home_dir_pattern="/home/$user" fi mapfile -t files < <(compgen -G "${home_dir_pattern%/}") home="${files[0]}" if [ ! -d "$home" ]; then home="$home_dir_pattern" echo "$a2 Directory not found: '$home'." while true; do echo "" read -rp "$p Do you want to create this directory? [Y/n] " create_home case "$create_home" in [Yy]|"" ) echo "$a2 Creating home directory: '$home'." mkdir -p "$home" || exit 1 echo "$a2 Selected home directory." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done else echo "$a2 Found directory: '$home'." while true; do echo "" read -rp "$p Do you want to continue with this directory? [Y/n] " use_home case "$use_home" in [Yy]|"" ) echo "$a2 Selected home directory: '$home'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done fi done echo "$a2 Creating user." if useradd -d "$home" "$user" &> /dev/null; then echo "$a2 Setting ownership." chown -R "$user:$user" "$home" echo "$a2 Set a password for user '$user':" passwd "$user" echo "$a1 Created user '$user' with home directory: '$home'." else echo "$a1 Failed to create user '$user'." fi break;; [Nn]|"" ) echo "$a1 Skipping..."; break;; * ) echo "$a1 Invalid response.";; esac done echo "" echo "This user will own the data directory and will run the Minecraft server (i.e." echo "execute Java). Ensure that the selected user will have the ability to do so." while true; do echo "" read -rp "$p Enter a user: " user echo "$a1 Checking for user '$user'..." if id "$user" &> /dev/null; then echo "$a2 Found user: '$user'." while true; do echo "" read -rp "$p Do you want to continue with this user? [Y/n] " use_user case "$use_user" in [Yy]|"" ) echo "$a1 Selected user '$user'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done else echo "$a2 Could not find user: '$user'." fi done # Data directory. echo "" echo "The data directory stores the minecraft directories (minecraft/ and sometimes" echo "minecraft.bak/) and some generated scripts. The minecraft directories store the" echo "server itself and all of the game data." echo "All server files (excluding systemd units and command symlinks) will be within" echo "the specified data directory. This does NOT allow having multiple servers," echo "especially when separate Java versions or systemd units are needed." echo "Running multiple servers would require changing various names, commands, and" echo "file paths, which goes beyond the scope of this script. Instead, using separate" echo "machines via a hypervisor is recommended and is the intended use of this script." echo "" echo "The data directory can be specified as any directory on the system, but must be" echo "specified as an absolute file path. Using a relative file path will cause issues" echo "later on." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the data directory path: " data_dir_pattern mapfile -t files < <(compgen -G "${data_dir_pattern%/}") data_dir="${files[0]}" if [ ! -d "$data_dir" ]; then data_dir="$data_dir_pattern" echo "$a1 Directory not found: '$data_dir'." while true; do echo "" read -rp "$p Do you want to create this directory? [Y/n] " create_data_dir case "$create_data_dir" in [Yy]|"" ) echo "$a2 Creating data directory: '$data_dir'." mkdir -p "$data_dir" || break echo "$a2 Selected data directory." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done else echo "$a1 Found directory: '$data_dir'." echo "NOTICE: Keep in mind that multiple steps involve removing directories based on" echo " this file path. A malformed or incorrect value could be very bad." while true; do echo "" read -rp "$p Do you want to continue with this directory? [Y/n] " use_dir case "$use_dir" in [Yy]|"" ) echo "$a1 Selected data directory: '$data_dir'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done fi done echo "$a2 Setting ownership." chown "$user:$user" "$data_dir" # Install/update a server. echo "" echo "Choose to install a new server or update an existing one." echo "WARNING: Only update an existing server if it was initially installed using this" echo " script, or if you know that it's been set up in the same way." echo "NOTICE: This will replace any existing minecraft.bak/ directory." while true; do echo "" read -rp "$p Update an existing server? [y/N] " update case "$update" in [Yy] ) update="true" echo "$a1 Updating..." break;; [Nn]|"" ) update="false" echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done echo "$a1 Changing working directory to '$data_dir'." cd "$data_dir" || exit 1 if [ "$update" = "true" ]; then req_failed="" some_failed="" if [ -d "minecraft.bak" ]; then echo -n "$a2 Removing previous minecraft.bak directory... " if rm -rf "minecraft.bak/"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi fi echo -n "$a2 Backing up current minecraft directory... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif mv "minecraft/" "minecraft.bak/"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi echo -n "$a2 Creating new minecraft directory... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif mkdir -p "minecraft"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi echo -n "$a2 Copying world... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/world/" "minecraft/world/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying mods... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/mods/" "minecraft/mods/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying config... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/config/" "minecraft/config/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying defaultconfigs... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/defaultconfigs/" "minecraft/defaultconfigs/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying properties and player/IP lists... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp "minecraft.bak/banned-ips.json" "minecraft.bak/banned-players.json" "minecraft.bak/ops.json" "minecraft.bak/server.properties" "minecraft.bak/whitelist.json" "minecraft/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Updating ownership... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif chown -R "$user:$user" "minecraft"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi if [ "$req_failed" = "true" ] || [ "$some_failed" = "true" ]; then while true; do echo "" read -rp "$p At least one step failed, do you want to proceed anyway? [y/N] " skip_fail case "$skip_fail" in [Yy] ) echo "$a1 Ignoring failures..."; break;; [Nn]|"" ) echo "$a1 Exiting. Please try again or resolve failures manually."; exit;; * ) echo "$a1 Invalid response.";; esac done fi echo "$a1 Finished copying data." echo "NOTICE: After completing the installation, please check for other config files" echo " or directories that you may want to keep such as logs, configurations," echo " mod-specific directories, etc." echo "NOTICE: Mods must be updated manually, please do so before starting the server." echo "$a1 Changing working directory to '$data_dir/minecraft'." cd "$data_dir/minecraft" || exit 1 else existing_dir="" echo "$a1 Creating minecraft directory..." if [ -d "minecraft" ]; then echo "$a2 The minecraft directory already exists in: '$data_dir'." echo "" echo "If this directory is already backed up or is no longer needed and you plan to" echo "set up the server again or use a new server, then it should be removed. You will" echo "have a few seconds to stop the script if you choose to remove the directory." while true; do echo "" read -rp "$p Remove directory before continuing? [y/N] " rm_dir case "$rm_dir" in [Yy] ) echo "$a2 Removing: '$data_dir/minecraft' in 10 seconds." sleep 5 echo "$a2 5."; sleep 1 echo "$a2 4."; sleep 1 echo "$a2 3."; sleep 1 echo "$a2 2."; sleep 1 echo "$a2 1."; sleep 1 echo "$a2 Removing minecraft directory." rm -rf "minecraft" || exit 1 break;; [Nn]|"" ) echo "$a2 Using existing minecraft directory." existing_dir="true" break;; * ) echo "$a1 Invalid response.";; esac done fi if [ "$existing_dir" = "true" ]; then echo "$a2 Updating ownership." else echo "$a2 Creating minecraft directory." mkdir -p "$data_dir/minecraft" echo "$a2 Setting ownership." fi chown "$user:$user" "$data_dir/minecraft" echo "$a2 Changing working directory to '$data_dir/minecraft'." cd "$data_dir/minecraft" || exit 1 if [ "$existing_dir" = "true" ]; then echo "$a1 Using minecraft directory." else echo "$a1 Created minecraft directory." fi fi # Install Java. echo "" echo "Choose a valid version of Java." echo "Here is a table of Minecraft versions and their minimum Java requirements:" echo " Minecraft | Java" echo " <= 1.5.2 | 5 (1.5.0)" echo " 1.6.1 - 1.11.2 | 6 (1.6.0)" echo " 1.12 - 1.16.5 | 8 (1.8.0)" echo " 1.17 - 1.17.1 | 16" echo " 1.18 - 1.20.4 | 17" echo " 1.20.5 - 1.21.11 | 21" echo " 26.1 | 25" echo "Running Java versions newer than the requirements is possible but may result in" echo "instability, especially with the oldest versions. It is not recommended." echo "" echo "Ensure the version is available as a jdk(version)-openjdk package at" echo "https://archlinux.org/packages/?q=jdk, or it must be installed manually." echo "Make sure to check out the AUR for more versions." while true; do echo "" read -rp "$p Enter a valid version (ENTER to skip): " java case "$java" in "" ) echo "$a1 Skipping installation..." break;; *[!0-9]* ) echo "$a1 Invalid response.";; * ) echo "$a1 Installing Java $java via the OpenJDK package..." echo "$a2 Installing jdk$java-openjdk." if pacman -Sy --noconfirm "jdk$java-openjdk"; then echo "$a1 Installed OpenJDK $java." break else echo "$a1 Failed to install OpenJDK $java." fi ;; esac done # Installer. echo "" echo "Choose if you want to install a Fabric server. You should still choose 'Y' if" echo "you are re-running this script. Downloading and running the installer can still" echo "be skipped in the next step." echo "If you are installing a Fabric server with your own installer, then choose 'N'" echo "and proceed like any other manual installation." echo "The Fabric installer supports Minecraft 1.14 and later." while true; do echo "" read -rp "$p Install a Fabric server? [Y/n] " use_fabric case "$use_fabric" in [Yy]|"" ) install="fabric" break;; [Nn] ) echo "$a1 Skipping Fabric installer." echo "" echo "Choose if you want to use an existing server installer." echo "This requires an existing and accessible .jar file." while true; do echo "" read -rp "$p Use other server installer? [Y/n] " use_other case "$use_other" in [Yy]|"" ) install="manual" break;; [Nn] ) install="none" runjar="" echo "$a1 Skipping installation." break;; * ) echo "$a1 Invalid response.";; esac done break;; * ) echo "$a1 Invalid response.";; esac done # Installation. if [ "$install" != "none" ]; then if [ "$install" = "fabric" ]; then while true; do echo "" read -rp "$p Download the Fabric installer? [Y/n] " get_installer case "$get_installer" in [Yy]|"" ) get_installer="true" break;; [Nn] ) echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done if [ "$get_installer" = "true" ]; then echo "$a1 Getting Fabric installer..." echo "$a2 Checking latest installer version." installer_ver=$(curl -s https://maven.fabricmc.net/net/fabricmc/fabric-installer/maven-metadata.xml | grep -oPm1 '(?<=<latest>)[^<]+') echo "$a2 Got installer version: '$installer_ver'." echo "$a2 Downloading file." curl -s -o 'fabric-installer.jar' "https://maven.fabricmc.net/net/fabricmc/fabric-installer/$installer_ver/fabric-installer-$installer_ver.jar" echo "$a1 Downloaded installer." while true; do echo "" read -rp "$p Select valid Minecraft version to install (ENTER for latest): " mc case "$mc" in "" ) mc="latest" echo "$a1 Installing latest Fabric server..." echo "$a2 Running installer." if sudo -u "$user" java -jar "fabric-installer.jar" server -downloadMinecraft; then break else echo "$a1 Failed to install server." fi ;; * ) echo "$a1 Installing $mc Fabric server..." echo "$a2 Running installer." if sudo -u "$user" java -jar "fabric-installer.jar" server -mcversion "$mc" -downloadMinecraft; then break else echo "$a1 Failed to install server." fi ;; esac done echo "$a1 Installed $mc Fabric server." echo "$a2 Removing installer." rm "fabric-installer.jar" fi elif [ "$install" = "manual" ]; then echo "" echo "Enter the server installer file path." echo "Working directory is '$data_dir/minecraft' for relative file paths." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the server installer path: " serverjar_pattern mapfile -t files < <(compgen -G "$serverjar_pattern") serverjar="${files[0]}" if [ ! -f "$serverjar" ]; then serverjar="$serverjar_pattern" echo "$a1 File not found: '$serverjar'." else echo "$a1 Found file: '$serverjar'." while true; do echo "" read -rp "$p Do you want to continue with this installer? [Y/n] " use_installer case "$use_installer" in [Yy]|"" ) echo "$a1 Selected server installer: '$serverjar'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done fi done echo "" echo "Choose server installer arguments. These are NOT the runtime arguments and will" echo "have no effect after the installation is complete." echo "Forge should use 'nogui --installServer' args." echo "You can and should use '--nogui' on newer versions." while true; do echo "" read -rp "$p Enter any installer arguments (ENTER for 'nogui'): " server_args if [ ! "$server_args" ]; then server_args="nogui" fi echo "" echo "Server installer arguments: '$server_args'" while true; do echo "" read -rp "$p Do you want to continue with these arguments? [Y/n] " use_install_args case "$use_install_args" in [Yy]|"" ) use_install_args="true" echo "$a1 Selected server installer arguments: '$server_args'." break;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done if [ "$use_install_args" = "true" ]; then echo "$a1 Installing server..." echo "$a2 Launching executable." if sudo -u "$user" bash -c "java -jar \"$serverjar\" $server_args"; then echo "$a1 Finished installing the server." break else echo "$a1 Failed to install the server." fi fi done echo "$a2 Removing installer." rm "$serverjar" fi # File generation. while true; do echo "" echo "Normally, the server would be run once to generate a eula.txt and other config" echo "files to help set up the server. However, this is entirely optional." while true; do echo "" read -rp "$p Do you want to launch the server once? [Y/n] " launch_once case "$launch_once" in [Yy]|"" ) break;; [Nn] ) echo "$a1 Skipping..."; break 2;; * ) echo "$a1 Invalid response.";; esac done different_exec="" if [ "$install" = "fabric" ]; then runjar="fabric-server-launch.jar" echo "$a2 Checking for '$runjar'." if [ ! -f "$runjar" ]; then echo "" echo "The Fabric server launch file could not be found. Either specify an existing" echo "launch file or skip the first launch and continue until an executable is needed," echo "like if you are going to generate a run script." while true; do echo "" read -rp "$p Do you want to specify a different executable? [y/N] " different_exec case "$different_exec" in [Yy] ) different_exec="true" break;; [Nn]|"" ) echo "$a1 Skipping first launch..." break 2;; * ) echo "$a1 Invalid response.";; esac done fi fi if [ "$install" = "manual" ] || [ "$different_exec" = "true" ]; then echo "" echo "Enter the server executable file path (not the installer)." echo "Working directory is '$data_dir/minecraft' for relative file paths." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the .jar file that runs the server (ENTER for *.jar): " runjar_pattern if [ ! "$runjar_pattern" ]; then runjar_pattern="*.jar" fi mapfile -t files < <(compgen -G "$runjar_pattern") runjar="${files[0]}" if [ ! -f "$runjar" ]; then runjar="$runjar_pattern" echo "$a2 File not found: '$runjar'." else echo "$a2 Found file: '$runjar'." while true; do echo "" read -rp "$p Do you want to continue with this executable? [Y/n] " use_exec case "$use_exec" in [Yy]|"" ) echo "$a2 Selected server executable: '$runjar'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done fi done fi echo "" echo "Choose server executable arguments. These are NOT saved and will only be used" echo "this one time to launch the server so that config files are generated." echo "You can and should use '--nogui' on newer versions." while true; do echo "" read -rp "$p Enter any executable arguments (ENTER for 'nogui'): " run_args if [ ! "$run_args" ]; then run_args="nogui" fi echo "" echo "Server executable arguments: '$run_args'" while true; do echo "" read -rp "$p Do you want to continue with these arguments? [Y/n] " use_run_args case "$use_run_args" in [Yy]|"" ) echo "$a2 Selected server executable arguments: '$run_args'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done done echo "$a2 Running server..." sudo -u "$user" bash -c "java -jar \"$runjar\" $run_args" echo "$a2 Finished running server." echo "" echo "If there were any errors (other than the expected EULA notice), you may want to" echo "stop here and make some changes. You can press Ctrl+C if you need to exit this" echo "script, or enter 'Y' to just re-enter the executable path and arguments." echo "Keep in mind that you do not want the server to launch fully, (which it won't" echo "as long as eula.txt is left alone,) as the only purpose of this is to generate" echo "files like a eula.txt, and ideally other configs for you to modify later." while true; do echo "" read -rp "$p Do you want to retry the installation? [y/N] " do_retry case "$do_retry" in [Yy] ) echo "$a1 Retrying..." break;; [Nn]|"" ) echo "$a1 First server launch completed." break 2;; * ) echo "$a1 Invalid response.";; esac done done # EULA. echo "$a1 Setting EULA..." echo "" echo "If no EULA file exists, one will be created with a single line: 'eula=true'." while true; do echo "" read -rp "$p Agree to the EULA? [Y/n] " eula case "$eula" in [Yy]|"" ) if [ ! -f "eula.txt" ]; then echo "$a2 Created eula.txt." echo "eula=true" > "eula.txt" else echo "$a2 Modified eula.txt to set: 'eula=true'." sed -i 's/^eula=.*/eula=true/' "eula.txt" fi echo "$a1 EULA set to true." break;; [Nn] ) echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done fi # Run script. echo "" echo "Do you want to create a script which launches the server with set arguments?" echo "It will be stored in the minecraft directory. Modify that file to change the" echo "arguments that are used every time the server is launched." echo "Skipping both its creation and usage will prevent systemd unit creation." while true; do echo "" read -rp "$p Create run.sh script? [Y/n] " make_script case "$make_script" in [Yy]|"" ) run_script="install" break;; [Nn] ) echo "" echo "For a run script to be \"existing\", it must be named 'run.sh' and be within the" echo "minecraft directory. Ensure this is true if it must be used." while true; do echo "" read -rp "$p Use existing run.sh script? [Y/n] " use_script case "$use_script" in [Yy]|"" ) run_script="existing" break;; [Nn] ) run_script="skip" echo "$a1 Skipping..." break;; * ) echo "$a2 Invalid response.";; esac done break;; * ) echo "$a2 Invalid response.";; esac done if [ "$run_script" = "install" ]; then echo "$a1 Creating run script with Java arguments..." echo "" echo "Choose allocated server memory in GB" echo "You should leave roughly 1-2GB available for the host. E.g. use up to 6-7GB if" echo "the system has 8GB. I recommend using 3-6 for best performance, and avoiding >8" echo "unless there are many players or mods." while true; do echo "" read -rp "$p Enter allocated memory [1-99] (ENTER to use 4GB): " mem case "$mem" in [1-9]|[1-9][0-9] ) echo "$a2 Allocating ${mem}GB." break;; "" ) echo "$a2 Allocating 4GB." mem="4" break;; * ) echo "$a2 Invalid response.";; esac done if [ ! "$runjar" ]; then echo "$a2 No .jar file has been selected to run the server." echo "" echo "Enter the server executable file path (not the installer)." echo "Working directory is '$data_dir/minecraft' for relative file paths." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the .jar file that runs the server (ENTER for *.jar): " runjar_pattern if [ ! "$runjar_pattern" ]; then runjar_pattern="*.jar" fi mapfile -t files < <(compgen -G "$runjar_pattern") runjar="${files[0]}" if [ ! -f "$runjar" ]; then runjar="$runjar_pattern" echo "$a2 File not found: '$runjar'." else echo "$a2 Found file: '$runjar'." fi while true; do echo "" read -rp "$p Do you want to continue with this executable path? [Y/n] " use_exec case "$use_exec" in [Yy]|"" ) echo "$a2 Selected server executable: '$runjar'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done done fi echo "" echo "You can and should use '--nogui' on newer versions." while true; do echo "" read -rp "$p Do you want to use '--nogui' instead of 'nogui' in the launch script? [y/N] " new_nogui case "$new_nogui" in [Yy] ) nogui_arg="--nogui" break;; [Nn]|"" ) nogui_arg="nogui" break;; * ) echo "$a1 Invalid response.";; esac done echo "$a2 Using '$nogui_arg' argument in launch script." echo "$a2 Writing script." cat > "$data_dir/minecraft/run.sh" <<- EOF cd "$data_dir/minecraft" java -Xmx${mem}G -Xms${mem}G -XX:+UseG1GC -Dsun.rmi.dgc.server.gcInterval=2147483646 -XX:+UnlockExperimentalVMOptions -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M -jar "$runjar" $nogui_arg EOF echo "$a2 Setting ownership." chown "$user:$user" "$data_dir/minecraft/run.sh" echo "$a2 Marking executable flag." chmod +x "$data_dir/minecraft/run.sh" echo "$a1 Created launch script in the minecraft directory." fi # Systemd units. sysd="" if [ "$update" != "true" ] && [ "$run_script" != "skip" ]; then echo "" echo "Choose to set up autostart by creating systemd unit files." echo "This would allow you to start/stop the server with systemctl, view logs with" echo "journalctl, and send commands to Minecraft's server console in the background." while true; do echo "" read -rp "$p Do you want to set up a systemd unit? [Y/n] " sysd case "$sysd" in [Yy]|"" ) sysd="install" break;; [Nn] ) sysd="skip" echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done fi if [ "$sysd" = "install" ] && [ "$run_script" != "skip" ]; then echo "$a1 Creating systemd unit files..." echo "$a2 Creating service file." cat > "/etc/systemd/system/minecraft.service" <<- EOF [Unit] Description=Dedicated Minecraft server After=network.target minecraft.socket Requires=minecraft.socket [Service] User=$user Type=exec WorkingDirectory=$data_dir/minecraft ExecStart=/usr/bin/sh -c "$data_dir/minecraft/run.sh < /run/minecraft.control" ExecStop=/usr/bin/sh -c "echo '/stop' > /run/minecraft.control" Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target EOF echo "$a2 Creating socket file." cat > "/etc/systemd/system/minecraft.socket" <<- EOF [Unit] Description=Minecraft control FIFO PartOf=minecraft.service [Socket] ListenFIFO=/run/minecraft.control SocketMode=0660 SocketUser=$user RemoveOnStop=true [Install] WantedBy=multi-user.target EOF echo "$a1 Created systemd unit files." echo "$a2 Reloading systemd daemon." systemctl daemon-reload echo "$a2 Enabling socket for FIFO creation." systemctl enable minecraft.socket &> /dev/null echo "$a2 Enabling service." systemctl enable minecraft.service &> /dev/null echo "$a1 Enabled systemd service." echo "INFO: The server is set up to start and restart automatically." echo "$a1 Creating mcc (Minecraft console) script in the data directory..." echo "$a2 Creating script." cat > "$data_dir/mcc.sh" <<- EOF #!/bin/bash if [ ! -p /run/minecraft.control ]; then echo "FIFO does not exist." exit 1 fi timestamp="\$(date +"%F %T.%N")" journalctl -o cat -fu minecraft.service --since "\$timestamp" & journal_pid=\$! printf "%s\n" "\$*" > /run/minecraft.control wait \$journal_pid EOF echo "$a2 Setting ownership." chown "$user:$user" "$data_dir/mcc.sh" echo "$a2 Marking executable flag." chmod +x "$data_dir/mcc.sh" echo "$a1 Created console script in data directory." echo "INFO: The console script passes all arguments to the server, and returns the" echo " output until stopped with Ctrl+C." add_cmd="" echo "" echo "Choose to install an mcc.sh link in /usr/bin/." echo "Creates a link to ~/mcc.sh, allowing you to run server commands from anywhere" echo "using just that command. E.g. 'mcc /gamerule keepInventory true'." echo "You will choose the command name." while true; do echo "" read -rp "$p Add mcc.sh link in /usr/bin? [Y/n] " add_cmd case "$add_cmd" in [Yy]|"" ) while true; do echo "" read -rp "$p Enter the name of the command (ENTER for 'mcc'): " mcc_cmd if [ ! "$mcc_cmd" ]; then mcc_cmd="mcc" fi if command -v "$mcc_cmd" &> /dev/null; then echo "$a2 Command '$mcc_cmd' already exists." if [ "$(command -v "$mcc_cmd")" = "/usr/bin/$mcc_cmd" ]; then echo "" echo "The command '$mcc_cmd' appears to be the file: '/usr/bin/$mcc_cmd'." if [ -L "/usr/bin/$mcc_cmd" ]; then echo "It links to '$(readlink "/usr/bin/$mcc_cmd")'." else echo "It is not a symbolic link. It was not created by this script." fi while true; do echo "" read -rp "$p Do you want to replace it? [y/N] " rm_cmd case "$rm_cmd" in [Yy] ) echo "$a2 Removing: '/usr/bin/$mcc_cmd'." rm -f "/usr/bin/$mcc_cmd" break 2;; [Nn]|"" ) break;; * ) echo "$a1 Invalid response.";; esac done else echo "" echo "It is not a file in the /usr/bin directory. It was not created by this script." echo "The path is: $(command -v "$mcc_cmd")" while true; do echo "" read -rp "$p Do you want to create a link in /usr/bin anyway? [y/N] " do_conflict case "$do_conflict" in [Yy] ) break 2;; [Nn]|"" ) break;; * ) echo "$a1 Invalid response.";; esac done fi continue 2 else echo "$a2 Command '$mcc_cmd' is available." while true; do echo "" read -rp "$p Do you want to continue with this name? [Y/n] " use_cmd case "$use_cmd" in [Yy]|"" ) echo "$a1 Selected command: '$mcc_cmd'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done fi done add_cmd="true" echo "$a2 Creating symlink." ln -s "$data_dir/mcc.sh" "/usr/bin/$mcc_cmd" echo "$a1 Created '$mcc_cmd' link." break;; [Nn] ) add_cmd="false" echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done fi if [ "$install" != "none" ]; then echo "" echo "Installation complete, the server is not running." echo "NOTICE: Please configure server.properties before starting the server." if [ "$install" = "fabric" ]; then echo "NOTICE: It's recommended to download performance mods such as Lithium," echo " FerriteCore, ModernFix, and possibly others." fi if [ "$run_script" != "skip" ]; then if [ "$sysd" = "install" ]; then echo "" echo "Systemd is in use, the server will automatically start after boot." echo "NOTICE: The server can be stopped by using either systemd, the /stop command," echo " or shutting down the host. Each method performs a clean shutdown, so the" echo " server won't lose data. You may notice a delay shutting down the host as" echo " the Minecraft server stops." echo "" echo "Many commands are available with systemd, some important ones are:" echo "Executing commands in the Minecraft server console using a script:" echo " $ ./mcc.sh <command here>" if [ "$add_cmd" = "true" ]; then echo "Or by using the '$mcc_cmd' command:" echo " # $mcc_cmd <minecraft command here>" fi echo "View the server logs with journalctl: (Shift+G jumps to the end)" echo " $ journalctl -u minecraft" echo "Or follow them as they update with:" echo " $ journalctl -fu minecraft" echo "And easily perform a clean start/stop of the server with:" echo " # systemctl {start,stop,restart} minecraft" else echo "" echo "Start the server with the run script, or set up a user crontab for autostart." echo " $ ./run.sh" fi fi fi echo "$a1 Finished." More resources