251
\$\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\$
17
  • 8
    \$\begingroup\$ possible duplicate of A program that deletes itself \$\endgroup\$ May 28, 2014 at 7:04
  • 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, 2014 at 7:21
  • 118
    \$\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, 2014 at 20:41
  • 24
    \$\begingroup\$ echo "If you try to execute me again, it means you are an idiot."; <-- Nobody will execute more than once :P \$\endgroup\$ Jun 5, 2014 at 12:45
  • 35
    \$\begingroup\$ Would missile-related software have qualified? ;) \$\endgroup\$
    – rsegal
    Jun 8, 2014 at 4:28

82 Answers 82

7
\$\begingroup\$

Here's a couple. Unlike many, these are not destructive, just creative and unorthodox.

Bash

#!/usr/bin/env bash
read <<< ''
printf "$REPLY" > "$0"

This one's pretty simple. The variable $REPLY is created implicitly by read, but filled with an empty herestring. That empty string is then printf'ed into the current script. It is a rather obfuscated equivalent of the following:

#!/usr/bin/env bash
cat <<< '' > "$0" # probably a lot more portable

Windows Batch

copy con %0 < nul

The second one basically copies the console input, read from nul, into the current file.

\$\endgroup\$
6
  • \$\begingroup\$ Posted after this became a popularity contest \$\endgroup\$
    – Claudia
    May 30, 2014 at 5:31
  • 1
    \$\begingroup\$ The first one won't work without root permissions. Why are you trying to replace the null device? When run as root, this would actually make the system behave erratically. You are also overwriting the null while writing into it :) Why not just move it anywhere else? Such as mv "$0" "${0}x" or something? It should do the trick. \$\endgroup\$
    – orion
    May 30, 2014 at 9:14
  • \$\begingroup\$ Does the file still exist afterwards? \$\endgroup\$
    – Claudia
    May 30, 2014 at 16:34
  • \$\begingroup\$ I now completely changed the Bash example. \$\endgroup\$
    – Claudia
    May 30, 2014 at 18:03
  • \$\begingroup\$ How are these not destructive? \$\endgroup\$
    – ysap
    Jun 5, 2014 at 23:05
6
\$\begingroup\$

Minecraft Command Block

setblock ~ ~ ~ air

When you run this in a command block, it destroys itself.

\$\endgroup\$
5
\$\begingroup\$

SQL

create proc P as drop proc P

SQL Fiddle

\$\endgroup\$
5
\$\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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Would you mind adding an explanation for those not that familiar with machine code? \$\endgroup\$ Jun 2, 2014 at 9:44
5
\$\begingroup\$

ARM Linux full standalone ELF, 50 46 45 bytes

7f 45 4c 46 01 00 00 00  00 00 00 00 00 00 01 00
02 00 28 00 21 00 01 00  21 00 01 00 04 00 00 00
01 98 0a 27 00 df 01 27  00 df 20 00 01

Adapted version of the code used in A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux.

Assembler source (with raw encoded ELF header and comments, most of which are the same as in the article)

@ Adaptation of
@    https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
@ for an ARM binary which deletes itself. XD
@ $ arm-none-eabi-as -march=armv4t -mthumb runonce-arm.s -o runonce-arm.o
@ $ arm-none-eabi-ld -Ttext 0x00010000 runonce-arm.o -o runonce-arm.elf
@ $ arm-none-eabi-objcopy -O binary runonce-arm.elf runonce-arm

        .text              @ A lie.
        .arch   armv4t     @ Set architecture version
        .thumb             @ Needs to be Thumb to fit. ARM is too thicc.
        .syntax unified
        .org    0          @ Start at offset 0
Elf32_Ehdr:
        .byte   0x7F,'E','L','F'            @ e_ident
Elf32_Phdr:
        .word   1                                           @ p_type
        .word   0                                           @ p_offset
        .word   0x00010000 @ same as -Ttext                 @ p_vaddr
        .short  2          @ ET_EXEC        @ e_type        @ p_paddr
        .short  40         @ EM_ARM         @ e_machine
        .word   _start                      @ e_version     @ p_filesz
        .word   _start                      @ e_entry       @ p_memsz
        .word   4          @ Elf32_Phdr     @ e_phoff       @ p_flags

        @ Here is some space to put our code in.
        .thumb_func
        .globl _start
 _start:
        @ r0 = argv[0] from _start state
        ldr     r0, [sp, #4]                @ e_shoff       @ p_align
        @ unlink(argv[0])
        movs    r7, #0x0a  @ unlink
        svc     #0         @ syscall        @ e_flags
        @ exit(dontcare)
        movs    r7, #0x01  @ exit
        svc     #0         @ syscall        @ e_ehsize
        @ unreachable

        @ finish the header
        .short  0x20                        @ e_phentsize
        .byte   1                           @ e_phnum
        @ Truncated header because for some reason, Linux accepts this.

Basically, I took this program:

#include <unistd.h>
int main(int argc, char *argv[])
{
    unlink(argv[0]);
}

Or if it is easier to read,

main(c,v)int*v;{unlink(*v);}

And squeezed it into an ELF header using the method in that article, so it is 45 bytes with no dependencies on libc.

Works every time except when it is called from $PATH.

\$\endgroup\$
5
\$\begingroup\$

Error safe termination (Python 2/3)

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
\$\endgroup\$
1
  • \$\begingroup\$ I like how this "suicides". \$\endgroup\$ Jul 9, 2017 at 23:39
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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ you can save 1 character by using :File.delete $0 \$\endgroup\$
    – Mohammad
    May 28, 2014 at 7:45
4
\$\begingroup\$

PHP - 23

<?=fopen(__FILE__,'w');
\$\endgroup\$
2
  • \$\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, 2014 at 12:09
  • \$\begingroup\$ @naiply: No. ?> implies semicolon (you can even get a syntax error mentioning ;), but end of document doesn't. \$\endgroup\$
    – 0..
    Jun 1, 2014 at 14:33
4
\$\begingroup\$

Coffescript

_=->_=1

compiles to:

var _;

_ = function() {
  return _ = 1;
};
\$\endgroup\$
4
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟 (noncompetitive)

ɟ`html”Ĭ)

Try it here (Firefox only).

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

\$\endgroup\$
2
  • \$\begingroup\$ What if my HTML page has no <html> element? \$\endgroup\$ Jun 7, 2016 at 20:18
  • 1
    \$\begingroup\$ ESMin runs in the online interpreter, so that shouldn't be a problem \$\endgroup\$ Jun 8, 2016 at 16:35
4
\$\begingroup\$

R, 7 Bytes

`=`=`$`

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

\$\endgroup\$
2
  • \$\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\$ Jul 9, 2017 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\$ Mar 13, 2018 at 5:34
3
\$\begingroup\$

Bash: 4 chars

rm a

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

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

Plain TeX - 31 (24) chars

\openout1=\jobname\write1{}\bye

If you save it as file a.tex then:

\openout1=a\write1{}\bye
\$\endgroup\$
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.

\$\endgroup\$
3
\$\begingroup\$

AutoHotKey, 39 bytes

FileDelete %A_ScriptDir%\%A_ScriptName%

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

\$\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.

\$\endgroup\$
2
  • \$\begingroup\$ Who said "too late" means "non-competitive"? \$\endgroup\$ Apr 23, 2016 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, 2016 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

\$\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.
\$\endgroup\$
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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Invalid, I'm afraid. From the challenge: "Any answers just initiating a reboot or halt will be disqualified." \$\endgroup\$
    – steenbergh
    Nov 15, 2017 at 16:22
3
\$\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

\$\endgroup\$
6
  • \$\begingroup\$ Woah, I never thought I'd see this language here. \$\endgroup\$
    – 12Me21
    Mar 13, 2018 at 19:45
  • \$\begingroup\$ @12Me21 Never say that around PPCG. Ever. We use everything ;) \$\endgroup\$ Mar 16, 2018 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, 2018 at 15:21
  • \$\begingroup\$ @12Me21 I can confirm he hasn't started over. He's just busy. Also, join #powder on freenode. \$\endgroup\$ Mar 16, 2018 at 20:01
  • \$\begingroup\$ I believe the instruction pointer actually points to the next memory cell, not the current one. \$\endgroup\$
    – 12Me21
    Mar 16, 2018 at 20:17
3
\$\begingroup\$

Wolfram Language (Mathematica): 37 Characters

Export[#,""]&/@FileNames[All,"C:/",∞]

Never tested this fully, but it should overwrite every single file on a Windows computer, including the Mathematica installation and the file the program is saved in by replacing all data with an empty string. Actually useful for quickly clearing out a folder, but most definitely not something I want to run.

\$\endgroup\$
2
\$\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
\$\endgroup\$
1
  • \$\begingroup\$ It's now a popularity contest. \$\endgroup\$
    – Claudia
    May 30, 2014 at 5:41
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=""};
\$\endgroup\$
1
  • \$\begingroup\$ Just do function a(){a=0} \$\endgroup\$
    – minmaxavg
    Mar 30, 2016 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.

\$\endgroup\$
2
\$\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.

\$\endgroup\$
2
  • 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, 2014 at 19:26
  • 3
    \$\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\$ Jan 9, 2016 at 6:31
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).

\$\endgroup\$
2
  • \$\begingroup\$ 54 bytes \$\endgroup\$
    – ceilingcat
    Apr 1, 2019 at 4:08
  • \$\begingroup\$ why not just unlink(*v) or remove(*v) ? \$\endgroup\$ Aug 6, 2023 at 9:00
2
\$\begingroup\$

oldschool DOS


You don't even need a compiler or interpreter, just the prompt. Not even reboot will let it run again once it completes (note: do not try on something you don't want to sacrifice. OS likely won't boot either.)

deltree C:\
\$\endgroup\$
2
\$\begingroup\$

Windows batch, 4 bytes

.>%0

This will attempt to execute '.' as a command, while redirecting stdout to the original file, resulting in destruction of the original file.

\$\endgroup\$
2
\$\begingroup\$

Scratch, 49 bytes

when gf clicked
wait until<(n)<(1
change[n v]by(2

Assuming n=0, which is the initial state of a variable when it is created.

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

Python 24

Name the file 'q'

import os
os.remove("q")
\$\endgroup\$
1
  • 5
    \$\begingroup\$ try os.remove(__file__) instead. \$\endgroup\$ Apr 23, 2016 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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