Google Cloud Servers: Unity says “Magic number is wrong: 542”

This apparently is a mono bug, and seems to happen if you try to invoke a Linux headless build on a Google Cloud VPS instance. From what I understand it’s something to do with the terminal emulator that the Google Cloud Console uses.

If you used SSH to log into the server and started that way, I assume everything will work correctly. Here’s a bit of a log that causes Unity to cease operation after throwing the exceptions:

Set current directory to /home/matt
Found path: /home/matt/CodenameMisfits.x86_64
Mono path[0] = '/home/matt/CodenameMisfits_Data/Managed'
Mono config path = '/home/matt/CodenameMisfits_Data/MonoBleedingEdge/etc'
Preloaded 'NativeNamedPipe.so'
Preloaded 'libAudioPluginDissonance.so'
Preloaded 'libenet.so'
Preloaded 'libopus.so'
Initialize engine version: 2018.4.14f1 (05119b33d0b7)
Forcing GfxDevice: Null
GfxDevice: creating device client; threaded=0
NullGfxDevice:
Version: NULL 1.0 [1.0]
Renderer: Null Device
Vendor: Unity Technologies
Begin MonoManager ReloadAssembly
Exception: Magic number is wrong: 542
at System.TermInfoReader.ReadHeader (System.Byte[] buffer, System.Int32& position) [0x00028] in <2fa7a6a452ca43d$ at System.TermInfoReader..ctor (System.String term, System.String filename) [0x0005f] in <2fa7a6a452ca43df998f07$ at System.TermInfoDriver..ctor (System.String term) [0x00055] in <2fa7a6a452ca43df998f07fd1486c0df>:0
at System.ConsoleDriver.CreateTermInfoDriver (System.String term) [0x00000] in <2fa7a6a452ca43df998f07fd1486c0df$ at System.ConsoleDriver..cctor () [0x0004d] in <2fa7a6a452ca43df998f07fd1486c0df>:0
Rethrow as TypeInitializationException: The type initializer for 'System.ConsoleDriver' threw an exception.
at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding) [0x0000$
at System.Console..cctor () [0x0008e] in <2fa7a6a452ca43df998f07fd1486c0df>:0
Rethrow as TypeInitializationException: The type initializer for 'System.Console' threw an exception.
at UnityEngine.UnityLogWriter.Init () [0x00006] in :0
at UnityEngine.ClassLibraryInitializer.Init () [0x00001] in :0
(Filename: <2fa7a6a452ca43df998f07fd1486c0df> Line: 0)

The fix is to run your server with the following:

TERM=xterm ./Your-Linux-Build.x86_64

Once you’ve done that, your Unity headless instance will boot normally and do what you’ve programmed it to do without a head on its shoulders. Mission complete.