# Challenge: Write a piece of code that quits itself [closed]

I'm searching (am I?) for a piece of code that quits immediately - in an absolutely unconventional way.

This does not mean: System.exit((int) 'A'); (Java).

It might mean:

#!/usr/bin/env python3
# NOTE: This kills ALL RUNNING Python processes. Be careful!
def exit():
import os
os.system("killall python3")
exit()


Most upvoted answer wins! All languages, all architectures.

Edit: Quitting by throwing exceptions won't be accepted any more!

• But thats not unconventional to end execution... Well, it's an alternative to exit(), but still it's an implemented feature... Dec 28 '13 at 21:25
• This isn't code-trolling - we know we want weird answers from this one. Dec 28 '13 at 22:36
• Does shutting the system down work?
– user10766
Dec 29 '13 at 0:42
• I once accidentally caused a network card to DMA over the operating system. When it happened you were instantly back in the BIOS, rebooting. Dec 29 '13 at 0:52
• I feel like Shannon has us beat here ;) Dec 29 '13 at 6:33

# z80 assembly

    ld hl, $0DF0 ;hl now holds 0xF00D because little-endian loop: push hl jr loop ;Assembles into 21 F0 0D E5 18 FD  Everyone knows that even processors need to eat sometimes, so this program gives it food; the quitting process could be sped up by writing ld sp, <value> at the beginning, as SP is the stack pointer register, but the z80 will get more food this way. ### 2 bytes  di ;disable interrupts halt ;wait for interrupt ;Assembles into F3 76  This one will quit sooner or later. Probably later. • > quits immediately-- I guess it's immediate on a cosmic scale. May 25 '15 at 5:37 • By the way, F3 76 is only two bytes, as is 18 00 (jr 00). There may be something I'm missing, as I don't program in z80 assembly. May 25 '15 at 5:55 • @ThomasKwa Oh whoops, you're right. I made this post right before I went to sleep. May 25 '15 at 16:08 • @ThomasKwa Forgot to mention-- that second one wouldn't work, since jr is relative (±128 bytes). The non-relative jump, jp, takes up two bytes on its own, unfortunately. May 25 '15 at 20:55 # AppleScript, 4 bytes quit The reason for this syntax existing is that the "quit" command is directed towards the current object, and is typically used as such: tell application "Finder" quit end tell However, since we have not specified an object to tell to quit, it defaults to the top-level scripting object, the code itself (or the window through which it is being run). Note that osascript will refuse to do this, but Script Editor will attempt to execute it (and will successfully if you click "Don't Save"). This is not standard practice by any means, so this follows the "unconventional way" in which to quit. # Python def bigRedButton(): for x in range(0, 10): print("Oh dear, Armageddon nuclear detonation in: " + str(x) + " seconds") import sys sys.exit()  • One quick question though (I dont know too much python sorry!), how does sys.exit() differ from os.system(kill)? I noticed another python answer using that. Dec 1 '15 at 1:38 # Batch File (Quits in 2 Chars :P) cd  ## Java throw new ThreadDeath();  or int i = 1; i /= 0;  Boring answer. Just add an other (non-daemon) thread and it will not quit. • We should forbid throwing exceptions. Dec 28 '13 at 21:26 • I agree... edited challenge Dec 28 '13 at 21:27 # C Can compile statically - portable across different kernels with different syscall assignments. #include <unistd.h> #include <stdio.h> #include <string.h> #include <bits/wordsize.h> main(int argc, char **argv) { FILE *fp; char buf[BUFSIZ], *s; sprintf(buf, "/usr/include/asm/unistd_%d.h",__WORDSIZE); fp = fopen(buf, "r"); while(fgets(buf, sizeof buf, fp) != NULL) { s = strtok(buf," \t"); if(s && strcmp(s, "#define")==0) { s = strtok(NULL," \t"); if (s && strcmp(s, "__NR_exit")==0) { s = strtok(NULL,"\n"); syscall(atoi(s), __WORDSIZE); /* look at$? to verify this call was executed */
}
}
}
}


python (though any other language would do)

import os
os.command('shutdown -r 0')


Only works on certain linux flavors, needs to be executed as root.

C code

#include <stdlib.h>
int main(void) {
system("shutdown.exe -s -f -t 0");
}


# Perl (13 bytes)

$/.=$/while 1


Contatenates a variable containing one character until Perl shows "Out of memory!" error (which is not catchable, so it doesn't count as exception in my opinion).

## Spoon

00101111

• ? this looks strange, please give a hint on what's going on. Jul 9 '14 at 15:15
• @CousinCocaine 00101111 = Immediately terminate program execution Jul 9 '14 at 18:04
• Spoon is to much for me than! Jul 9 '14 at 23:16

# DOS .com, 2 bytes

f4 fa

Not only the program but the whole machine will quit!

cli; hlt

C#

public class Program
{
static void Main(string[] args)
{
// lets quit --- yaayyy
}
}


C#

Not sure it would count as a valid answer. But it will eventually quit.

void quit() {
quit();
}


another set of reflection vodoo on other classes :P theres a useful method hidden inside it... exercise to the reader: understand the haxx i used

Edit: forgot a lookup statement

class Svoujnf
{

public static void main(String[] arggxes)
{
char[] lm = new Svoujnf().getClass().getName().toCharArray();
for (char c : lm)
{
c=(char)(c-1);
}
try
{
Class.forName(new String(lm)).getMethod("exit",Integer.class ).invoke(Class.forName(new String(lm)).getMethod("get"+new String(lm), null).invoke(null, null), 0);
}
catch (Exception ex)
{
ex.printStackTrace();
}

}
}


Its not system :P

another way:

class Svoujnf
{

//Runtime.
public static void rageeeeee() {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe u= (Unsafe) f.get(null);
long l = u.allocateMemory(1024);
long l2 = u.allocateMemory(1024);
u.copyMemory(l,l2, 1024000);

} catch (Exception e) {
}
}
public static void main(String[] arggxes)
{

rageeeeee();
}
}

• your class/variable/function names are making me cringe
– cat
Nov 30 '15 at 23:55
• In one of those snippets the class name contains data :P Dec 1 '15 at 6:26
• Clever, I hadn't noticed that!
– cat
Dec 1 '15 at 10:17

C#

class Program
{
static void Main(string[] args)
{
}
}


# Rebol - 1

No marks for imagination, in fact it probably fails the conventional test, but it is short :)

q


q is a native function that stops evaluation and exits the interpreter. Synonymous with quit.

• You could at least help me with my political quest to make Q not be defined to QUIT with an example like append [n o p] q...! Aug 13 '14 at 2:04

# Python 2.7

code = type((lambda: 0).func_code)
crasher = code(0, 0, 0, 0, "\x01", (), (), (), "crasher", "crasher", 0, "\0")
exec crasher


Creates a code object and then runs it. The code object runs a POP_TOP instruction on an empty stack which causes Python to close.

Oneliner if you prefer: exec type((lambda: 0).func_code)(0, 0, 0, 0, "\x01", (), (), (), "crasher", "crasher", 0, "\0")

LUA 3 Chars, one of the shortest, so far

i()


(I would prefer die() for fun, but i() is shorter)

How it works: It doesn't, because i() doesn't exist. This is why the script stops.

Javascript

while(true)eval(prompt("", ""));


Runs whatever the user enters. Will most likely result in a runtime syntax error.

## Java

Because calling System.exit() directly is for n00bz.

import java.math.BigInteger;
import java.lang.reflect.*;

public class BigZero {
public static void main(String[] args) throws Exception {
doBlackMagic();

System.out.println("Big zero is " + BigInteger.ZERO);
}

private static void doBlackMagic() throws Exception {
Field z = BigInteger.class.getField("ZERO");
Field m = Field.class.getDeclaredField("modifiers");
m.setAccessible(true);
m.setInt(z, z.getModifiers() & ~Modifier.FINAL);
z.set(null, new EvilBigInteger("0"));
}

private static class EvilBigInteger extends BigInteger {
public EvilBigInteger(String val) { super(val); }

@Override
public String toString() { System.exit(0); return null; }
}
}


# Mathematica

$Pre = Unevaluated@Quit  Executing this will cause Quit to be evaluated before any subsequent evaluation. That is, any subsequent program will quit the kernel. It seems pretty unconventional that we can now crash the kernel by running Print["Hello, World!"]  # Vitsy, 2 natural ways, 1 byte each ;  This ends the current block or method. Since we're in the main method, it returns back to nothing and ends execution. x  Exit immediately with the exit code as defined by the integer representation of the top item modulo 256. # Underhanded ways ### w/ Bash 'killall java'r,  Should be pretty obvious. ### w/ TryItOnline <  Ends execution after 60 seconds. # Powershell kill$pid


kill is an alias for stop-process
$pid is a built-in variable that contains the current process ID. Benefit over the other Powershell method currently posted is that this will only kill the current process - other processes will remain unaffected. ## Bash Got the idea from https://codegolf.stackexchange.com/a/4403/8766 kill -9 $$ # BASH kill -9 BASHPID  This would kill only the shell where it resides, whereas kill -9$$ would kill not only itself, but also any parent shells. From BASH man page: Expands to the process ID of the current bash process. This differs from$\$ under certain circumstances, such as subshells that do not require bash to be re-initialized.

Delphi:

procedure EndIt;
begin
halt;
end;


procedure Halt ( { ExitValue : Integer } ) ;

• looks quite conventional, as far as i know, halt just exits the process, which is exactly what this question is not about Jan 5 '14 at 5:13
• I'm not sure what unconventional means. You could write something that leaks memory in a tight loop, or causes continual access violations, that would freeze everything in short order. As for halt:"The Halt procedure forces an abrupt termination of the current application. Warning : resources cannot be guaranteed to be freed when calling halt." That's why I chose it over "application.terminate" - halt is extreme and what I considered to unconventional - rarely if ever, do you use it in production code. Jan 5 '14 at 5:35
• >Delphi >production code; whatever, I'm pretty sure this counts as conventional, other than that any kernel nowadays cleans the allocated memory after a dead process. Be more creative! Jan 5 '14 at 5:43
• @mniip - Be more creative! ROFMLAO - I'd have to crank up my Windows machine and see... enjoy... Jan 5 '14 at 5:46
• Breaking the machine is what quarter of the entries are about Jan 5 '14 at 5:49