So, after a lot of digging and testing, I came to the conclusion that it’s not possible… -ish!
I came across a few weird behaviors while testing different events and approaches, first;
process.on('SIGINT', (code) => {
console.log('Process event SIGINT triggered with code:', code);
});
works-ish … but not reliably, normally, it wouldn’t get triggered using “Ctrl-C” or “quit” command, but with time and a bit of frustration from failing, I spammed “Ctrl-C” a few dozens of times to force it to quit quickly and …
the SIGINT
was triggered as shown below…
On the first “Ctrl-C” a message will pop up in the console saying: “Press Ctrl-C again (or type quit
) to exit.”
and after pressing “Ctrl-C” again for the second time confirming it, this will pop up: " Quitting: SIGINT received"
Now, from my testing, the SIGINT
event can ONLY be triggered if you spam “Ctrl-C” in between the two messages, meaning it will only work, if you press “Ctrl-C” after the first message, but before the second message, weird!
I’ve also tried spawning a child process and delay the process exit enough to log the stuff I need, but this method also suffers the same fate, it’s not reliable, and will ONLY be triggered if you spam “Ctrl-C” in between the two messages, same as first method.
something worthy of noting is that using the ‘quit’ command does not trigger the SIGINT
event at all, no matter how fast you spam it or how many times you can execute the command, whether spamming it my hand or using a file and executing it using the exec
command!
But; while I messing around with TxAdmin, I noticed that the SIGINT
event suddenly started working quite reliably and consistently, not skipping a single “Ctrl-C”, unless, you only do “Ctrl-C” once in the TxAdmin console, because Tx:Logger
will detect that the server is closed after a minute or so and will restart it automatically, but the second “Ctrl-C” will immediately close the terminal without a chance to trigger a single event!
I’ve also tried digging in the FiveM source code to try and figure something out, but let’s just say… it was too complicated for me and I don’t really understand C++'s syntax, so I came out empty handed.
so; in conclusion, detecting a server shutdown (without using TxAdmin) is not possible because there isn’t any way of detecting process exit reliably and consistently, thus; making using the combination of:
- The
on('onResourceStop')
event - to detect resource stops/restarts (credit: DigitalDeluxeGamer) - The
on('txAdmin:events:serverShuttingDown')
event - to detect scheduled/intentional shutdowns/restarts (credit: Zerio) - The
process.on('SIGINT')
event (with TxAdmin) - to detect Ctrl-C in TxAdmin console
the only reliable way, and that only leaves one thing undetectable, or for now at least, which is a server crash… although TxAdmin can do an awesome job detecting em’ and restart the server automatically, but my resource is out of luck, because the resource won’t be able to listen for txAdmin:events:serverShuttingDown
when it gets triggered as noted in the events page for TxAdmin .
also note that while using TxAdmin, the ‘quit’ command also does not trigger any of the events listed above, but TxAdmin will detect that the server is down when it’s not supposed to be down, and will restart it automatically!
and for anyone needing the code to do those 3 events, here it is:
process.on('SIGINT', (code) => {
RSRCINT('Process event SIGINT triggered with code: ' + code);
});
on('onResourceStop', resourceName => {
if(GetCurrentResourceName() === resourceName) RSRCINT('CitizenFX event onResourceStop triggered')
});
on('txAdmin:events:serverShuttingDown', details => {
RSRCINT('TxAdmin event serverShuttingDown triggered')
});
/**
* Resource Interrupt function to gracefully handle shutdowns
* @param {string} msg the event message
* @returns {void}
*/
const RSRCINT = msg => console.log(`^1Resource Interruption Detected! (${msg})^0`)
Thanks to everyone who suggested, recommended, and/or contributed to any possible solution, and happy coding my friends!