37
votes
\$\begingroup\$

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")
    # Windows addon
    os.system("taskkill /im python.exe /f")
exit()

Most upvoted answer wins! All languages, all architectures.

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

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

86 Answers 86

1 2
3
1
vote
\$\begingroup\$

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.

\$\endgroup\$
1
vote
\$\begingroup\$

Python

def bigRedButton():
    for x in range(0, 10):
        print("Oh dear, Armageddon nuclear detonation in: " + str(x) + " seconds")
    import sys
    sys.exit()
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 1, 2015 at 1:38
1
vote
\$\begingroup\$

Batch File (Quits in 2 Chars :P)

cd
\$\endgroup\$
0
votes
\$\begingroup\$

Java

throw new ThreadDeath();

or

int i = 1;
i /= 0;

Boring answer. Just add an other (non-daemon) thread and it will not quit.

\$\endgroup\$
2
  • \$\begingroup\$ We should forbid throwing exceptions. \$\endgroup\$ Commented Dec 28, 2013 at 21:26
  • \$\begingroup\$ I agree... edited challenge \$\endgroup\$
    – s3lph
    Commented Dec 28, 2013 at 21:27
0
votes
\$\begingroup\$

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 */
            }       
        }   
    }
}
\$\endgroup\$
0
votes
\$\begingroup\$

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.

\$\endgroup\$
0
votes
\$\begingroup\$

C code

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

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).

\$\endgroup\$
0
votes
\$\begingroup\$

Spoon   

00101111
\$\endgroup\$
3
  • \$\begingroup\$ ? this looks strange, please give a hint on what's going on. \$\endgroup\$ Commented Jul 9, 2014 at 15:15
  • \$\begingroup\$ @CousinCocaine 00101111 = Immediately terminate program execution \$\endgroup\$
    – Timtech
    Commented Jul 9, 2014 at 18:04
  • \$\begingroup\$ Spoon is to much for me than! \$\endgroup\$ Commented Jul 9, 2014 at 23:16
0
votes
\$\begingroup\$

DOS .com, 2 bytes

f4 fa

Not only the program but the whole machine will quit!

cli; hlt

\$\endgroup\$
0
votes
\$\begingroup\$

C#

public class Program
{
   static void Main(string[] args)
   {
         // lets quit --- yaayyy
   }
}
\$\endgroup\$
0
votes
\$\begingroup\$

C#

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

void quit() {
    quit();
}
\$\endgroup\$
0
votes
\$\begingroup\$

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();
    }
}   
\$\endgroup\$
3
  • \$\begingroup\$ your class/variable/function names are making me cringe \$\endgroup\$
    – cat
    Commented Nov 30, 2015 at 23:55
  • \$\begingroup\$ In one of those snippets the class name contains data :P \$\endgroup\$
    – masterX244
    Commented Dec 1, 2015 at 6:26
  • \$\begingroup\$ Clever, I hadn't noticed that! \$\endgroup\$
    – cat
    Commented Dec 1, 2015 at 10:17
0
votes
\$\begingroup\$

C#

class Program
{
    static void Main(string[] args)
    {
        System.Threading.Thread.CurrentThread.Abort();
    }
}
\$\endgroup\$
0
votes
\$\begingroup\$

Bash

> >(:) set

It quits with broken pipe.

\$\endgroup\$
0
votes
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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...! \$\endgroup\$ Commented Aug 13, 2014 at 2:04
0
votes
\$\begingroup\$

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")

\$\endgroup\$
0
votes
\$\begingroup\$

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.

\$\endgroup\$
0
votes
\$\begingroup\$

Javascript

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

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

\$\endgroup\$
0
votes
\$\begingroup\$

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; }
    }
}
\$\endgroup\$
0
votes
\$\begingroup\$

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!"]
\$\endgroup\$
0
votes
\$\begingroup\$

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.

\$\endgroup\$
-1
votes
\$\begingroup\$

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.

\$\endgroup\$
-1
votes
\$\begingroup\$

Bash

Got the idea from https://codegolf.stackexchange.com/a/4403/8766

kill -9 $$
\$\endgroup\$
-1
votes
\$\begingroup\$

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.

\$\endgroup\$
-3
votes
\$\begingroup\$

Delphi:

procedure EndIt;
begin
   halt;
end;

procedure Halt ( { ExitValue : Integer } ) ;

\$\endgroup\$
10
  • 3
    \$\begingroup\$ looks quite conventional, as far as i know, halt just exits the process, which is exactly what this question is not about \$\endgroup\$
    – mniip
    Commented Jan 5, 2014 at 5:13
  • \$\begingroup\$ 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. \$\endgroup\$
    – Vector
    Commented Jan 5, 2014 at 5:35
  • 1
    \$\begingroup\$ >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! \$\endgroup\$
    – mniip
    Commented Jan 5, 2014 at 5:43
  • \$\begingroup\$ @mniip - Be more creative! ROFMLAO - I'd have to crank up my Windows machine and see... enjoy... \$\endgroup\$
    – Vector
    Commented Jan 5, 2014 at 5:46
  • \$\begingroup\$ Breaking the machine is what quarter of the entries are about \$\endgroup\$
    – mniip
    Commented Jan 5, 2014 at 5:49
1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.