228
\$\begingroup\$

Goal

The goal of this challenge is to write code that will execute once and only once. This means basically that it damages the program, script, or environment in some way. If rebooting the system allows the code to run again that is permitted.

Scoring

Number of votes. All assumptions must be clearly listed. Any answers just initiating a reboot or halt will be disqualified.

Additional Rules because Greg Hewgill is a demi-god

No root access is permitted.

End Date

The contest will close on May 31, 2014.

Edit

This contest has been changed to popularity contest.

\$\endgroup\$
  • 7
    \$\begingroup\$ @PeterTaylor a possible Solution would be the a self delete but as the result are showing it isn't the only one. \$\endgroup\$ – Lukei May 28 '14 at 7:21
  • 105
    \$\begingroup\$ To a lot of people calling vi in a single terminal environment has this effect, no escape from it unless you reboot :) Just a joke here. \$\endgroup\$ – orion May 28 '14 at 20:41
  • 6
    \$\begingroup\$ The site is free for you to propose such a question. \$\endgroup\$ – ojblass May 30 '14 at 22:40
  • 17
    \$\begingroup\$ echo "If you try to execute me again, it means you are an idiot."; <-- Nobody will execute more than once :P \$\endgroup\$ – user3459110 Jun 5 '14 at 12:45
  • 29
    \$\begingroup\$ Would missile-related software have qualified? ;) \$\endgroup\$ – rsegal Jun 8 '14 at 4:28

75 Answers 75

5
\$\begingroup\$

SQL

create proc P as drop proc P

SQL Fiddle

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin __FILE__
    len equ $ - source

SECTION .text
global _start

_start:

    pop rdi           ;number of parameters
    pop rdi           ;path to executable, parameter of unlink and open

    mov    rax, 87    ;unlink
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Compile with:

nasm -f elf64 FILENAME
ld -m elf_x86_64 FILENAME.o -o FILENAME

Or the same thing in C (with inline assembly):

#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}

When run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code.

There must be a better way to do this in C (or maybe not), but I don't know any.

Isn't that much better than just having it delete itself!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Funny way to not answer the challenge. +1 for hack value. \$\endgroup\$ – Stéphane Gourichon Jan 9 '16 at 6:34
  • \$\begingroup\$ The C version is not 64-bit specific or even architecture-specific, is it? So it is actually portable (x86 vs MIPS, ARM etc, Linux vs other OS even Windows), isn't it ? \$\endgroup\$ – Stéphane Gourichon Jan 9 '16 at 6:35
  • \$\begingroup\$ @StéphaneGourichon I think so, though I haven't tried it on anything by x86_64 linux. \$\endgroup\$ – Ian D. Scott Jan 9 '16 at 16:11
4
\$\begingroup\$

Ruby, 15 14

Put this line to a file (del.rb):

File.delete $0

then run it (self-destructive) : ruby del.rb del.rb

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ you can save 1 character by using :File.delete $0 \$\endgroup\$ – Mhmd May 28 '14 at 7:45
4
\$\begingroup\$

PHP - 23

<?=fopen(__FILE__,'w');
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I wonder if it is possible to drop the semicolon here. I know it's possible with short tags like this: <?= $var ?>. \$\endgroup\$ – nalply Jun 1 '14 at 12:09
  • \$\begingroup\$ @naiply: No. ?> implies semicolon (you can even get a syntax error mentioning ;), but end of document doesn't. \$\endgroup\$ – Konrad Borowski Jun 1 '14 at 14:33
4
\$\begingroup\$

Coffescript

_=->_=1

compiles to:

var _;

_ = function() {
  return _ = 1;
};
| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

x86 Machine Code (5 bytes)

HEX:

EA0000FFFF

ASM:

JMP FFFF:0000

You can try with debug.exe:

C:\>debug
-a 100
0AE7:0100 jmp ffff:0000
0AE7:0105
-g=100

Explanation: BIOSis always at FFFF:0000 in memory. So this sequence boots the computer, if this is run in a protected command prompt in Windows it makes that process unresponsive.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Would you mind adding an explanation for those not that familiar with machine code? \$\endgroup\$ – Martin Ender Jun 2 '14 at 9:44
4
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟 (noncompetitive)

ɟ`html”Ĭ)

Try it here (Firefox only).

Translates to $('html').remove() in Javascript.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ What if my HTML page has no <html> element? \$\endgroup\$ – NoOneIsHere Jun 7 '16 at 20:18
  • 1
    \$\begingroup\$ ESMin runs in the online interpreter, so that shouldn't be a problem \$\endgroup\$ – Mama Fun Roll Jun 8 '16 at 16:35
3
\$\begingroup\$

Bash: 4 chars

rm a

Put this in a file named a and run it on your Linux machine.

| improve this answer | |
\$\endgroup\$
  • 13
    \$\begingroup\$ rm * will work for every filename \$\endgroup\$ – Antonio Ragagnin May 28 '14 at 12:24
  • 3
    \$\begingroup\$ @AntonioRagagnin It's better to stick to non-destructive scripts \$\endgroup\$ – Daniel May 28 '14 at 12:52
  • 1
    \$\begingroup\$ rm $0 will also work for all filenames, at the cost of one character. \$\endgroup\$ – fNek May 31 '14 at 19:05
  • 7
    \$\begingroup\$ @AntonioRagagnin So will rm -rf / --no-preserve-root, but better ;) \$\endgroup\$ – Cole Johnson May 31 '14 at 21:49
  • 6
    \$\begingroup\$ @fNek No, it won't. Only for those without a whitespace in their name. \$\endgroup\$ – glglgl May 31 '14 at 21:57
3
\$\begingroup\$

Bash

This deletes the file and makes sure that you can't recover it. So it definitely can't execute more than once.

shred "$0"

shred is a program to securely delete files by overwriting them. It's in coreutils.

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

AutoHotKey, 39 bytes

FileDelete %A_ScriptDir%\%A_ScriptName%

(Yes, I'm aware this is an ancient thread)

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL (non-competing because it is too late)

f←⎕EX'f'∘⊣

f can only be called once. It needs an argument, which is ignored:

      f←⎕EX'f'∘⊣
      f 'dummy argument'
      f 'dummy argument'
VALUE ERROR
      f'dummy argument'
     ∧

It is a so-called "atop", which is a train of two functions. The left one is applied on the result of the right one.

The right function is 'f'∘⊣ which replaces any given argument with the string 'f'.

The left function is ⎕EX, which EXpunges the object named in its argument.

The first time around, 'f'∘⊣ returns 'f', which causes ⎕EX to erase f. The second time around, calling f results in an error because f does not exist.


Here is one that can be tried online:

f←{f∘←¨}

After defining the function, the first time that it is called (with a dummy argument), e.g. f 4, the function ignores the argument, and instead redefines itself to be an operator (higher-order function). Now, operators need at least a left operand so when it is called again, the call will fail with a syntax error.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Who said "too late" means "non-competitive"? \$\endgroup\$ – Erik the Outgolfer Apr 23 '16 at 7:54
  • \$\begingroup\$ @ΈρικΚωνσταντόπουλος Mama Fun Roll. But you did make me take a look at what I had written, and I found a typo. Thanks! \$\endgroup\$ – Adám Apr 25 '16 at 17:27
3
\$\begingroup\$

Linux, 8 bytes

killall5

Similar to this answer, but this works on pretty much any Linux since System V and you don't need to be root. Don't believe me? Try it out on your Linux system! You'll want to save any work first...

I actually found this by accident when I was 11 messing around with Linux for the first time.

Links: killall5 manpage, recording

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Commodore 64 BASIC, 7 bytes

POKE 1,3

I've had a lot of fun in various BASIC dialects POKE-ing around in the first few memory storage locations. Here's the effect of this one:

poke 1,3

Try it online! You'll have to type it in yourself.

Some other POKEs:

  • POKE 1,1 will freeze the system.
  • POKE 1,2 will clear the screen.
  • POKE 1,4 completely kills the system - the power button won't even work.
  • POKE 1,5 through POKE 1,9 do the same as POKE 1,4, but with varying levels of destruction.
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

R, 7 Bytes

`=`=`$`

The code makes the "=" (used for assigning) become the "$" (used for subseting). After that it gives error.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I don't think this is valid. If I put it in a file I can run it as many times as I want, right? \$\endgroup\$ – Esolanging Fruit Jul 9 '17 at 23:41
  • 1
    \$\begingroup\$ @EsolangingFruit, no on repeat execution this throws the error Error in `=` = `$` : object of type 'special' is not subsettable as validated in R version 3.4.3 (2017-11-30) \$\endgroup\$ – Taylor Scott Mar 13 '18 at 5:34
3
\$\begingroup\$

Error safe termination (Python 2)

from contextlib import contextmanager
@contextmanager
def suicide():
    try:    yield
    finally:
        open(__file__    , 'w');
        open(__file__+'c', 'w');

#--------------------------------------
# Error safe code ;)
#-------------------------------------
with suicide():
    print "Goodbye cruel world!"
    jump_off_building_______crash
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I like how this "suicides". \$\endgroup\$ – Esolanging Fruit Jul 9 '17 at 23:39
3
\$\begingroup\$

TIS-100, 6 bytes

@0
HCF

The HCF (or halt and catch fire) command instantly crashes the TIS-100. The only way to run it again is to start it up again.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Invalid, I'm afraid. From the challenge: "Any answers just initiating a reboot or halt will be disqualified." \$\endgroup\$ – steenbergh Nov 15 '17 at 16:22
2
\$\begingroup\$

Plain TeX - 31 (24) chars

\openout1=\jobname\write1{}\bye

If you save it as file a.tex then:

\openout1=a\write1{}\bye
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JAVASCRIPT (35 26 bytes when minified, in case that matters.) I know this is a late entry, but I had a situation where I considered being able to kill a function and decided it'd make a good entry here. (Still not sure if I'll actually use it.)

funny = function(){
  alert(0);
  window.funny = '';
};

funny();
funny();

minified

a=function(){alert(0);window.a=""};

Alert isn't really needed

a=function(){window.a=""};
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Just do function a(){a=0} \$\endgroup\$ – minmaxavg Mar 30 '16 at 22:05
2
\$\begingroup\$

Python 2.7

import inspect
import os
for x in range(0, 2):
    os.remove(inspect.stack()[0][1])
    print "am I dead yet?"

It only runs 1/2 of a time.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

C, 56 chars

x[9];main(c,v)int**v;{sprintf(x,"rm %s",*v);system(x);}

Run on a UNIX system. By convention, the first string param passed to a C program is the executable name. This program simply deletes the executable (but leaves the source, which you have to recompile to run it again).

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

R16K1S60 Assembly

mov [ax], ax

Simply overrides itself with with the contents of AX. the mov instruction is only one word large, so it deletes itself. Depends on AX being 0 (most releases guarentee this)

Safe version

mov [0], ax

Depends on ax not being this specific instruction

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Woah, I never thought I'd see this language here. \$\endgroup\$ – 12Me21 Mar 13 '18 at 19:45
  • \$\begingroup\$ @12Me21 Never say that around PPCG. Ever. We use everything ;) \$\endgroup\$ – moonheart08 Mar 16 '18 at 6:53
  • \$\begingroup\$ I wonder if LBPHacker will ever release the new computer he made. I'm guessing he's either really busy or he's just started over. Anyway, it might be better to use mov [0], ax since it doesn't rely on ax being 0. \$\endgroup\$ – 12Me21 Mar 16 '18 at 15:21
  • \$\begingroup\$ @12Me21 I can confirm he hasn't started over. He's just busy. Also, join #powder on freenode. \$\endgroup\$ – moonheart08 Mar 16 '18 at 20:01
  • \$\begingroup\$ I believe the instruction pointer actually points to the next memory cell, not the current one. \$\endgroup\$ – 12Me21 Mar 16 '18 at 20:17
1
\$\begingroup\$

Python 24

Name the file 'q'

import os
os.remove("q")
| improve this answer | |
\$\endgroup\$
  • 4
    \$\begingroup\$ try os.remove(__file__) instead. \$\endgroup\$ – Erik the Outgolfer Apr 23 '16 at 7:50
1
\$\begingroup\$

Haskell - 80 bytes

Pre base-4.6.0.0 : May not work on Windows. This depends on how the program is invoked.

import System.Environment
import System.Directory
main=getProgName>>=removeFile

Post base-4.6.0.0 : A bit longer but always works.

import System.Environment
import System.Directory
main=getExecutablePath>>=removeFile
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It's now a popularity contest. \$\endgroup\$ – Isiah Meadows May 30 '14 at 5:41
1
\$\begingroup\$

PowerShell (37)

Save following code to a script file (test.ps1)

rm $MyInvocation.MyCommand.Definition

Run from command line:

PS > .\test.ps1

Script will automatically delete itself.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

AppleScript ; 17 chars

delete document 1

I was playing with more elegant solutions like this one :

set the contents of the front document to ""

But the action of delete document 1 is even more fun !

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ you know the is optional? so you can just write set contents of front document to "" \$\endgroup\$ – dkudriavtsev Oct 10 '16 at 19:51
1
\$\begingroup\$

Shell script (Linux Bash); 4 characters

rm *

Warning ! Don’t try this at home.

This shell script can be run only once. When run, this shell script will self-destruct.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ For non-bash users: * is a glob, which expands to a list of all the files in the current directory. so it will delete everything in the folder the script is in as well as itself \$\endgroup\$ – user16402 Jun 9 '14 at 19:26
  • 2
    \$\begingroup\$ This implicitly assumes that the process' "current directory" matches the script directory. The "run once" property will fail in other cases. Example: say script is in ~/a, current directory is ~/b. The script can be run with ../a/thescript and it will only try to delete in ~/b which won't delete the script (it's not in that directory). b may even be already empty before the script runs. \$\endgroup\$ – Stéphane Gourichon Jan 9 '16 at 6:31
1
\$\begingroup\$

beeswax, 12 bytes

_8F+++P]f1Fw

Save this program under the name ! and execute it.

              lstack                       gstack
_             [0,0,0]•                                          create bee
 8            [0,0,8]•                                          lstack 1st=8
  F           [8,8,8]•                                          all lstack=lstack 1st
   +++        [8,8,16]•                                         1st=1st+2nd
              [8,8,24]•                                         3 times
              [8,8,32]•
      P       [8,8,33]•                                         increment 1st
       ]      [8,8,2377900603251621888]•                        rotate bits of 1st by 2nd steps
        f                               [2377900603251621888]•  push lstack 1st ont gstack
         1    [8,8,1]•                                          lstack 1st=1
          F   [1,1,1]•                                          all lstack=lstack 1st
           w            write gstack to file. lstack 1st=bytes used for file name, lstack 2nd= used file content bytes.

If we look at the stack contents in hex, it gets clearer what’s happening:

lstack[8,8,33]• is lstack[0x0000000000000008,0x000000000000008,0x0000000000000021]• in hex.

If we rotate the bits of the 1st lstack value by 8 to the right, we get

lstack[8,8,2377900603251621888]•, which is

lstack[0x0000000000000008,0x000000000000008,0x2100000000000000]• in hex.

Instruction f pushes the 1st lstack value on the gstack:

gstack[0x2100000000000000]•

Now comes instruction w: First, the 4-byte words of the gstack get reinterpreted as a stack of UInt8 values:

[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21]•, in little endian order. The MSB is on top of the stack.

Instruction w takes the 1st lstack value as number of bytes taken for the file name, and the 2nd lstack value gives the number of bytes stored in the file. lstack[1,1,1]• means that 1 byte is taken as file name. 0x21 is the ASCII code for !, which is the name of the program itself. The next single byte (lstack 2nd), the value 0x00 is stored as file content, which is not executable as beeswax program.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Caché ObjectScript, 12 bytes

k ^rOBJ($zn)

Output:

SAMPLES>do ^test

SAMPLES>do ^test

DO ^test
^
<NOROUTINE> *test
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

q (14 bytes)

This program deletes itself when run.

hdel hsym .z.f
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Bash (7 characters)

TMOUT=1

It destroying the user session within a second by terminating the shell. See: man bash.

Example:

$ TMOUT=1
timed out waiting for input: auto-logout
Saving session...completed.

[Process completed]
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That's not really different from typing exit or ^D. The later key combination will send EOF to the shell making it 0 bytes. \$\endgroup\$ – kasperd Nov 7 '16 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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