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.
83 Answers 83
Vigil
Finally a usecase for Vigil!
def main():
raise Exception()
Excerpt from the "language specification":
It goes without saying that any function that throws an exception which isn't caught is wrong and must be punished.
...
If an oath is broken, the offending function [...] will be duly punished.
How?
Simple: it will be deleted from your source code.
The only way to ensure your program meets its requirements to absolutely forbid code that fails to do so. With Vigil, it will do this for you automatically.
There are other ways to do this, because Vigil provides the keywords implore
and swear
which are basically oaths to adhere to certain pre- and post-conditions:
def main():
swear 0 > 1
-
129\$\begingroup\$ No doubt, this deserves to win. \$\endgroup\$Matteo Italia– Matteo Italia2014年05月28日 10:44:53 +00:00Commented May 28, 2014 at 10:44
-
92\$\begingroup\$ Finally, a sufficiently vigilant language for the modern age! I'm going to start using it in production. All bugs must be purged. \$\endgroup\$Claudiu– Claudiu2014年05月28日 15:55:19 +00:00Commented May 28, 2014 at 15:55
-
15\$\begingroup\$ To me it seems that it is not necessarily the fault of the function that throws an uncaught exception, it could also be that the functions on the call stack are negligent for failing to catch it. It could even suggest the architecture of the whole application is flawed (Because perhaps there is no way to properly handle that exception without restructuring the code?) In short, the whole program should be deleted. \$\endgroup\$Jonathan Pullano– Jonathan Pullano2014年05月28日 18:23:08 +00:00Commented May 28, 2014 at 18:23
-
12\$\begingroup\$ @JonathanPullano you're not the first one to notice that ;) \$\endgroup\$Martin Ender– Martin Ender2014年05月28日 18:24:17 +00:00Commented May 28, 2014 at 18:24
-
37\$\begingroup\$ Additional points for swearing. A program making a confident false statement no doubt needs to be eliminated. \$\endgroup\$orion– orion2014年05月28日 20:49:02 +00:00Commented May 28, 2014 at 20:49
Pretty much any Linux distro, 9 chars
This one is a classic!
#!/bin/rm
Put this in a file and run it:
> ed
a
#!/bin/rm
.
wq foo
> ls
Mail mbox foo
> chmod 777 foo
> ./foo
> ls
Mail mbox
Aaand it's gone!
As to what is going on:
I'm really just running rm
! #!
is called a shebang. And if you put one of these followed by a path to some executable as the first line in your script, the program loader will execute your script file with whatever you wrote there - the default is usually #!/bin/bash
or #!/bin/sh
(Bourne-again shell / Bourne shell). The first argument passed to this script will be the filename itself (which is why you see so many solutions including %0
or 0ドル
and the likes in here); so by making my file contain #!/bin/rm
and running it, all I'm doing is passing the filename of my file to rm
.
-
13\$\begingroup\$ Without ever having used Linux myself, I think it deletes the script file. \$\endgroup\$Οurous– Οurous2014年05月28日 11:23:56 +00:00Commented May 28, 2014 at 11:23
-
67\$\begingroup\$ @NateKerkhofs the
#!
sequence starts a shebang, which identifies what interpreter you are going to use for the script - normally/bin/bash
. Here, Flonk has used/bin/rm
as an "interpreter".rm
is a program which deletes files. because Linux thinks that we wantrm
to be an interpreter, it passes the filename as an argument to it. therefore, the file deletes itself \$\endgroup\$user16402– user164022014年05月28日 11:27:28 +00:00Commented May 28, 2014 at 11:27 -
22\$\begingroup\$ This is hysterical \$\endgroup\$ojblass– ojblass2014年05月28日 13:13:45 +00:00Commented May 28, 2014 at 13:13
-
27\$\begingroup\$ @user80551 I don't need root access for this. \$\endgroup\$Flonk– Flonk2014年05月28日 13:38:36 +00:00Commented May 28, 2014 at 13:38
-
12\$\begingroup\$ hmm...
./foo mbox
\$\endgroup\$Izkata– Izkata2014年05月28日 16:06:13 +00:00Commented May 28, 2014 at 16:06
x86 binary, 4 bytes
F0 0F C7 C8
Assumption: Must be run on a P5 Pentium CPU.
The above instruction is commonly known as the F00F bug. It attempts to execute an invalid instruction, prefixed with lock.
This freezes the CPU up completely (not a halt nor a reboot) and it doesn't even require root access.
-
5\$\begingroup\$ Wow... that is really interesting. \$\endgroup\$ojblass– ojblass2014年05月28日 04:55:50 +00:00Commented May 28, 2014 at 4:55
-
34\$\begingroup\$ @JackM: It will, but that's explicitly allowed in the question. \$\endgroup\$Dennis– Dennis2014年05月28日 19:46:42 +00:00Commented May 28, 2014 at 19:46
-
37\$\begingroup\$ @JackM, that seems like a great way to stop the "script" from running. \$\endgroup\$Paul Draper– Paul Draper2014年06月01日 02:21:23 +00:00Commented Jun 1, 2014 at 2:21
-
8\$\begingroup\$ I remember when this bug was making the rounds. A kernelside workaround was found almost immediately -- it won't crash the computer unless you're running an OS without the patch. \$\endgroup\$zwol– zwol2014年06月02日 02:28:55 +00:00Commented Jun 2, 2014 at 2:28
-
6\$\begingroup\$ @RosLuP 209 different users, apparently. If you disagree, downvote and move on. \$\endgroup\$Dennis– Dennis2017年05月30日 17:41:12 +00:00Commented May 30, 2017 at 17:41
Bash, 5
>"0ドル"
Truncates itself to zero length.
If the filename doesn't contain spaces, >0ドル
works for 3 chars!
-
2\$\begingroup\$ Actually, the quotes are never needed for a variable used in a redirect filename like this, even if the program or path to the program contains spaces, due to a special case in shell parsing rules. So you can always do it in 3 chars! \$\endgroup\$apenwarr– apenwarr2014年06月08日 05:30:54 +00:00Commented Jun 8, 2014 at 5:30
-
3\$\begingroup\$ @apenwarr it says "0ドル: ambiguous redirect" when without quotes and the filename contains spaces \$\endgroup\$user16402– user164022014年06月08日 06:34:43 +00:00Commented Jun 8, 2014 at 6:34
-
3\$\begingroup\$ @apenwarr, it is needed if the shell that interprets that script is based on ksh88 or is bash not in posix mode. It's not about space, it's about any character of
$IFS
for bash and wildcard characters for bash and ksh88. A she-bang less script will usually be interpreted by the system's sh, but some shells including bash and ksh88 will interpret them in a child of themselves instead. \$\endgroup\$sch– sch2016年05月04日 12:55:36 +00:00Commented May 4, 2016 at 12:55
gzip
#!/bin/gzip
To much annoyance to people used to nondestructive commandline tools, gzip
by default ruins the original file, replacing it with a gzipped version (adding a suffix).
This is a variation on the #!/bin/rm
option, except this one is recoverable by manual human intervention (call gunzip
on it). As a special bonus, the resulting file is much longer than the original (the difference depends on the filename length).
Warning: location of gzip
may vary.
EDIT: as pointed out by WChargin, this is more portable:
#!/usr/bin/env gzip
The rest of the file can have any content. It's essentially a file that, when called, hides itself in a box and refuses to come out until you forcibly unpack it.
-
8\$\begingroup\$ I never thought of it before but you are absolutely correct. \$\endgroup\$ojblass– ojblass2014年05月29日 01:34:58 +00:00Commented May 29, 2014 at 1:34
-
\$\begingroup\$ very interesting \$\endgroup\$Grijesh Chauhan– Grijesh Chauhan2014年05月29日 07:54:33 +00:00Commented May 29, 2014 at 7:54
-
12
-
47\$\begingroup\$ +1 just for
It's essentially a file that, when called, hides itself in a box and refuses to come out until you forcibly unpack it.
You made my day, and it's 1am. \$\endgroup\$clapp– clapp2015年07月22日 08:19:41 +00:00Commented Jul 22, 2015 at 8:19 -
1\$\begingroup\$ It's essentially a file that, when called, hides itself in a box and refuses to come out until you forcibly unpack it. made me laughing. :D \$\endgroup\$Mischa– Mischa2017年07月26日 15:31:20 +00:00Commented Jul 26, 2017 at 15:31
Bash, (削除) 13 (削除ここまで) 12
Not the shortest, but it doesn't actually delete the file or make the system unusable.
chmod 0 "0ドル"
If the filename doesn't contain spaces, you can remove the quotes to save 2 chars.
Explanation
It removes all permissions (rwx
) from itself. When you attempt to run it, or even view its code, a second time, without manually restoring the permissions, it will say something like
bash: <FILENAME>: Permission denied
Restore the permissions with
chmod +rwx <FILENAME>
Old version
Only removes execute permissions, but one char longer (this was a code-golf question before it got changed to popularity-contest):
chmod -x "0ドル"
Sneaky version by Łukasz Niemier
#!/bin/chmod 0
-
4\$\begingroup\$
chmod 0
works too \$\endgroup\$gnibbler– gnibbler2014年05月28日 11:06:30 +00:00Commented May 28, 2014 at 11:06 -
\$\begingroup\$ @gnibbler thnx, added \$\endgroup\$user16402– user164022014年05月28日 11:09:15 +00:00Commented May 28, 2014 at 11:09
-
\$\begingroup\$ I think you can call the program
a
and save yet 3 chars:chmod 0 a
\$\endgroup\$yo'– yo'2014年06月03日 11:54:36 +00:00Commented Jun 3, 2014 at 11:54 -
7\$\begingroup\$ Even nicer
#!/bin/chmod 0
\$\endgroup\$Hauleth– Hauleth2014年06月05日 23:28:40 +00:00Commented Jun 5, 2014 at 23:28 -
1\$\begingroup\$ Take
chmod 0 *
for 9 bytes. \$\endgroup\$Titus– Titus2016年11月21日 14:06:41 +00:00Commented Nov 21, 2016 at 14:06
-
84\$\begingroup\$ Well, the rules say "Any answers just initiating a reboot or halt will be disqualified", but this doesn't just initiate a halt, because it also catches fire. Thus, +1! \$\endgroup\$Hakanai– Hakanai2014年06月02日 13:01:02 +00:00Commented Jun 2, 2014 at 13:01
-
\$\begingroup\$ Catch fire and no more cpu, so code will run once :)) \$\endgroup\$raspiduino– raspiduino2021年06月10日 07:10:49 +00:00Commented Jun 10, 2021 at 7:10
Shell + sed, 16 bytes
Not quite as destructive as some of the other answers ;-)
This script inserts a comment #
at the beginning of every line of itself:
sed -i s/^/#/ 0ドル
-
3\$\begingroup\$ How about
sed -i g 0ドル
? \$\endgroup\$seshoumara– seshoumara2016年09月03日 12:19:00 +00:00Commented Sep 3, 2016 at 12:19
Commodore 64 BASIC
This one doesn't delete the program.
1 POKE 2048,1
Self-destruct
According to the Commodore 64 memory map, address2048
is unused, but it must contain a value of 0
so that the BASIC program can be RUN.
-
1\$\begingroup\$ Does poke simply move a variable to a location? \$\endgroup\$ojblass– ojblass2014年05月29日 01:30:52 +00:00Commented May 29, 2014 at 1:30
-
15\$\begingroup\$ @ojblass yes, effectively.
poke a, b
is equivalent to the C code*((unsigned char *)a) = b;
. \$\endgroup\$Jules– Jules2014年05月29日 05:54:04 +00:00Commented May 29, 2014 at 5:54 -
4\$\begingroup\$ I'd like to visualise my FaceBook pokes that way! \$\endgroup\$Sunny R Gupta– Sunny R Gupta2014年06月06日 09:58:33 +00:00Commented Jun 6, 2014 at 9:58
-
3\$\begingroup\$ Oh wow... I remember PEEK and POKE from C64 BASIC! I was writing this stuff around 1977. \$\endgroup\$Fixee– Fixee2014年07月05日 19:34:55 +00:00Commented Jul 5, 2014 at 19:34
-
\$\begingroup\$ side note: A
0
byte is used to mark the end of code lines; so address 0x800 probably needs one to mark the beginning of the first code line. \$\endgroup\$Titus– Titus2018年03月13日 15:41:02 +00:00Commented Mar 13, 2018 at 15:41
Assumption: Running Solaris, logged in as root
killall
-
3\$\begingroup\$ does solaris really have such a thing? \$\endgroup\$ojblass– ojblass2014年05月28日 04:00:45 +00:00Commented May 28, 2014 at 4:00
-
24\$\begingroup\$ Yes, it does just what it says on the tin and kills all running processes. \$\endgroup\$Greg Hewgill– Greg Hewgill2014年05月28日 04:01:16 +00:00Commented May 28, 2014 at 4:01
-
\$\begingroup\$ one upvote for you! \$\endgroup\$ojblass– ojblass2014年05月28日 04:01:56 +00:00Commented May 28, 2014 at 4:01
-
11\$\begingroup\$ 1, The question mentions no root access is permitted. \$\endgroup\$user80551– user805512014年05月28日 13:36:53 +00:00Commented May 28, 2014 at 13:36
-
133\$\begingroup\$ @user80551 he's the reason for that rule. \$\endgroup\$Seiyria– Seiyria2014年05月28日 14:58:02 +00:00Commented May 28, 2014 at 14:58
Batch: 5 Bytes
%0|%0
It is basically the forkbomb for Windows.
The app starts its first argument twice. Don't run it in a productive environment ;)
-
95\$\begingroup\$ Is there a productive windows environment? (I'm joking of course). \$\endgroup\$orion– orion2014年05月28日 20:25:49 +00:00Commented May 28, 2014 at 20:25
-
17\$\begingroup\$ it looks so innocent! \$\endgroup\$ojblass– ojblass2014年05月29日 13:01:33 +00:00Commented May 29, 2014 at 13:01
-
18\$\begingroup\$ Ok, definitely don't try that. It didn't go well. \$\endgroup\$rmobis– rmobis2014年06月03日 15:14:42 +00:00Commented Jun 3, 2014 at 15:14
-
82\$\begingroup\$ Don't have the rep to downvote, but it violates "runs only once" in the most severe way possible. \$\endgroup\$Xan– Xan2014年06月06日 09:53:56 +00:00Commented Jun 6, 2014 at 9:53
-
18\$\begingroup\$ I believe a human only executes it once and that it qualifies. \$\endgroup\$ojblass– ojblass2014年06月06日 12:17:42 +00:00Commented Jun 6, 2014 at 12:17
Python, 18
open(__file__,'w')
Truncates the file by opening it in write-only mode.
-
15\$\begingroup\$ Just in case you're serious, it opens itself (
__file__
refers to its own filename). Opening inw
mode truncates the file, basically clears it. \$\endgroup\$Bob– Bob2014年05月29日 17:50:06 +00:00Commented May 29, 2014 at 17:50 -
7\$\begingroup\$ I think the truncation happens on the open, but regardless the Python runtime should flush all open files at the end of the program, and either Python or the OS will close them when the process ends. 'course, it's generally good practice to explicitly close open handles when you're done with them. \$\endgroup\$Bob– Bob2014年05月29日 23:09:01 +00:00Commented May 29, 2014 at 23:09
-
6\$\begingroup\$ The "right" way to handle files in Python is with... er, with.
with open("myfile.txt","r") as thefile:
and then your file-handling code will automatically close the file when you're done. \$\endgroup\$Schilcote– Schilcote2014年05月31日 19:59:52 +00:00Commented May 31, 2014 at 19:59 -
1\$\begingroup\$ This could be shortened to something like
open("f.py","w")
. \$\endgroup\$Ethan Bierlein– Ethan Bierlein2015年06月18日 04:56:34 +00:00Commented Jun 18, 2015 at 4:56 -
3\$\begingroup\$ Doesn't cease to work whrn .pyc remains \$\endgroup\$minmaxavg– minmaxavg2016年04月10日 07:25:23 +00:00Commented Apr 10, 2016 at 7:25
JavaScript
localStorage.x = eval(localStorage.x + localStorage.x)
The first time you run it, it'll run fine:
>>> localStorage.x = eval(localStorage.x + localStorage.x)
NaN
If you try to run it more (even if you refresh), you'll get an error:
>>> localStorage.x = eval(localStorage.x + localStorage.x)
ReferenceError: NaNNaN is not defined
-
8\$\begingroup\$ So if you run it a lot of times, you get the Batman theme. +1 for that. \$\endgroup\$Hankrecords– Hankrecords2017年06月09日 10:59:04 +00:00Commented Jun 9, 2017 at 10:59
-
3\$\begingroup\$ @Hankrecords Sadly, not really, since this is not gonna execute later... \$\endgroup\$RedClover– RedClover2017年09月29日 14:55:43 +00:00Commented Sep 29, 2017 at 14:55
-
\$\begingroup\$ @Soaku Bummer... \$\endgroup\$Hankrecords– Hankrecords2017年09月29日 15:10:11 +00:00Commented Sep 29, 2017 at 15:10
-
\$\begingroup\$ @Hankrecords unless you remove
eval
, but then it won't fulfill this challenge \$\endgroup\$RedClover– RedClover2017年09月30日 16:16:27 +00:00Commented Sep 30, 2017 at 16:16
IBM's CMS Operating System, which is a single-user operating system which runs as a guest under IBM's VM Hypervisor, has an interesting file-structure.
Files consist of three elements, File Name, File Type, and File Mode. The File Mode consists of two elements, a single alphabetic, which for ease of explanation can be regarded in a similar way to the Drive Letter for Windows/MS-DOS, and a single numeric digit.
The single numeric digit has meaning, http://publib.boulder.ibm.com/infocenter/zvm/v5r4/index.jsp?topic=/com.ibm.zvm.v54.dmsa3/hcsd0b10127.htm, and for this task it is the number 3 which is interesting:
File Mode Number 3
File mode number 3 means that files are erased after they are read. You can use file mode number 3 if you do not want to maintain copies on your minidisks or in your SFS directories.
So, spend hours writing your script and file it as `LOST FOREVER A3'. Run it, it works first time. Set off home, job well done.
Note, no message is produced indicating the erasure. After all, everyone knows what that 3
means, don't they?
It is actually of course very useful. You can, once testing is complete, use the 3
for temporary files, and not have to clean up afterwards, because they are read-once files.
-
\$\begingroup\$ The link is dead and it's not on archive.org. \$\endgroup\$Oskar Skog– Oskar Skog2023年02月28日 08:53:02 +00:00Commented Feb 28, 2023 at 8:53
Bash , 12
Note: This is destructive.
:(){ :|:&};:
It's the popular bash fork-bomb. It exponentially eats all memory and PID's locking up the system. A hard-reboot will allow the code to be run again though why would you want to do that?
Bash , 10
:(){ :&};:
For two less chars, this eats your memory linearly.
Bash , 7
w;PATH=
w
is chosen as the shortest executable that I could think of.
This simply erases the path variable so the shell can't find /usr/bin/w
the next time. Restarting the shell fixes it as the path is usually stored in ~/.profile
-
\$\begingroup\$ Darn it :D I just had the same idea :P \$\endgroup\$Knerd– Knerd2014年05月28日 08:22:10 +00:00Commented May 28, 2014 at 8:22
-
2\$\begingroup\$
:(){ :&};:
shouldn't eat your memory. It stops as soon as it has forked, so there's only everO(1)
active. You've donewhile(fork());
, what would eat your memory linearly is the equivalent offork(); while(1);
. \$\endgroup\$marinus– marinus2014年05月28日 09:28:18 +00:00Commented May 28, 2014 at 9:28 -
32\$\begingroup\$ Wow! those are some deadly emoticions! I best not call my computer bignosed frownyface
:(){
.... \$\endgroup\$Level River St– Level River St2014年05月28日 22:27:45 +00:00Commented May 28, 2014 at 22:27 -
1\$\begingroup\$ If I remember correctly... the fork bomb periodically leaves one PID open. The script can be run again in that interval... and if that doesn't count, it is possible to kill all the forks with some effort. Rebooting is simply the easiest solution. \$\endgroup\$Brilliand– Brilliand2014年05月30日 21:53:51 +00:00Commented May 30, 2014 at 21:53
-
14\$\begingroup\$ I don't think this qualifies. It doesn't make it impossible for the script to run again, instead it guarantees that it runs again (many times). \$\endgroup\$Ben Voigt– Ben Voigt2014年05月31日 19:45:11 +00:00Commented May 31, 2014 at 19:45
-
1\$\begingroup\$ What's that do? \$\endgroup\$Darth Egregious– Darth Egregious2014年05月28日 16:52:13 +00:00Commented May 28, 2014 at 16:52
-
3\$\begingroup\$ @user973810 if i remember basic, new is for cleaning memory, so it will delete program \$\endgroup\$user902383– user9023832014年05月28日 17:08:32 +00:00Commented May 28, 2014 at 17:08
-
3\$\begingroup\$ Nice. Not specific to Commodore 64, probably works as is on almost every 8-bit BASIC machine from the 80's... \$\endgroup\$ysap– ysap2014年06月05日 22:56:34 +00:00Commented Jun 5, 2014 at 22:56
-
1\$\begingroup\$ You could OLD or O. it back too. I new'd 12 months of 12 year olds game coding genius ;p work by accident once. Then accidentally saved instead of old-ing. O. didnt work anymore :( I did learn how hex editors worked pretty sharpish though :) \$\endgroup\$Matt Warren– Matt Warren2014年06月08日 22:42:04 +00:00Commented Jun 8, 2014 at 22:42
-
3\$\begingroup\$ @ysap: It would be more portable if it were using 1 as line number; not all BASIC implementations supported line number 0. \$\endgroup\$celtschk– celtschk2014年06月22日 14:38:26 +00:00Commented Jun 22, 2014 at 14:38
C#
Shortest code for deleting self
Process.Start(new ProcessStartInfo("cmd.exe", "/C choice /C Y /N /D Y /T 3 & Del \"" + Assembly.GetExecutingAssembly().Location + "\""));
Code for making it unnoticeable in the UI
Process.Start( new ProcessStartInfo()
{
Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + Assembly.GetExecutingAssembly().Location+"\"",
WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, FileName = "cmd.exe"
});
-
7\$\begingroup\$ At last, a C# answer! \$\endgroup\$Pharap– Pharap2014年06月04日 17:44:56 +00:00Commented Jun 4, 2014 at 17:44
-
\$\begingroup\$ Why is choice required? \$\endgroup\$wizzwizz4– wizzwizz42016年05月05日 15:33:48 +00:00Commented May 5, 2016 at 15:33
-
\$\begingroup\$ @wizzwizz4: I'm guessing it's used for a delay (
/T 3
). The caller needs to exit before the executable can be deleted. \$\endgroup\$Oskar Skog– Oskar Skog2023年02月28日 07:59:04 +00:00Commented Feb 28, 2023 at 7:59
My Name's Shell...PowerShell
Classic self destructing message:
@'
##################################
Project X is a go
Meet Eagle 1 at WP Alpha
Further comms is hereby prohibited
##################################
This message will self destruct in
5 seconds
'@
5..1| %{$_;Sleep 1;}
"Goodbye"
rm $(.{$MyInvocation.ScriptName})
-
\$\begingroup\$ Explanation, please? \$\endgroup\$noɥʇʎԀʎzɐɹƆ– noɥʇʎԀʎzɐɹƆ2016年12月31日 17:37:05 +00:00Commented Dec 31, 2016 at 17:37
-
1\$\begingroup\$ @noɥʇʎԀʎzɐɹƆ Lines till 13 are just nonsense. After that, like any other shell, PowerShell loads the running script into memory and thus it no longer needs the original script file...so it deletes itself. \$\endgroup\$SomeShinyObject– SomeShinyObject2017年01月02日 00:59:24 +00:00Commented Jan 2, 2017 at 0:59
Bash (5)
rm 0ドル
Assuming you don't have spaces in the filename.
-
45\$\begingroup\$ This is why Unix is better than Windows: rm instead of del saved you a byte. \$\endgroup\$MadTux– MadTux2014年06月10日 19:04:02 +00:00Commented Jun 10, 2014 at 19:04
-
1\$\begingroup\$ @MadTux I prefer *nix to Unix. \$\endgroup\$wizzwizz4– wizzwizz42016年05月05日 15:36:23 +00:00Commented May 5, 2016 at 15:36
C++ 71
The executable file "golf" is denied permission to run next time.
#include <stdlib.h>
int main()
{
system("chmod a-x golf");
return 0;
}
JavaScript/HTML 137, 145 with console test
<script id="s">
function h(){
console.log(1);
var s=document.getElementById("s");
h=s.innerHTML;
h=h.replace("h","g");
s.innerHTML=h;
}
-
12\$\begingroup\$ @Templar in the context of JavaScript, I think that refreshing the page counts as rebooting (maybe?) \$\endgroup\$user16402– user164022014年05月28日 09:41:14 +00:00Commented May 28, 2014 at 9:41
-
2\$\begingroup\$ Even if you weren't removing the script from the page, it would still only execute once. There's no point to this. \$\endgroup\$nderscore– nderscore2014年05月28日 13:06:45 +00:00Commented May 28, 2014 at 13:06
-
2\$\begingroup\$ @nderscore Looks like the function
h
is what will only execute once. You have to actually call it from somewhere else (analogous to running a program, I guess). Say,<button onclick="h();">
. \$\endgroup\$Bob– Bob2014年05月30日 04:07:02 +00:00Commented May 30, 2014 at 4:07 -
1\$\begingroup\$ Bob, yes the function h must be called from onload or another event e.g onclick. \$\endgroup\$bacchusbeale– bacchusbeale2014年05月30日 12:12:36 +00:00Commented May 30, 2014 at 12:12
-
5\$\begingroup\$ Given that reloading the page is allowed, and your goal is only to prevent h from being executable more than once while it's already loaded, what's wrong with
function h(){h=0}
? \$\endgroup\$Keen– Keen2014年05月30日 18:59:57 +00:00Commented May 30, 2014 at 18:59
sh
#!/bin/sh
curl http://runonce.herokuapp.com/
Will run only once (and it shows "Hello, world!" if it runs), even if you reinstall the system, and put the script again.
EDIT: I figured I would release a script behind runonce as well. Nothing special, as it wasn't supposed to be released either way, but whatever.
<?php
$db = pg_connect("[connection information]");
$ip = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
pg_query("DELETE FROM users WHERE time < now() - interval '1 day'");
$result = pg_query_params($db, "SELECT * FROM users WHERE ip = 1ドル LIMIT 1", array($ip));
if (!pg_fetch_row($result)) {
echo "Hello, world!\n";
pg_query_params($db, "INSERT INTO users (ip) VALUES (1ドル)", array($ip));
}
As well as database schema
CREATE TABLE users (
ip inet PRIMARY KEY,
time timestamp with time zone NOT NULL DEFAULT now()
);
CREATE INDEX ON users (time);
The data is stored in Heroku's database, feel free to check their privacy policy if you like. I don't check the data stored in a database, it's stored purely to allow the code to execute once.
-
4\$\begingroup\$ I'd argue that this will run multiple times, it just produces no output after the first. \$\endgroup\$Brilliand– Brilliand2014年05月30日 21:56:36 +00:00Commented May 30, 2014 at 21:56
-
7\$\begingroup\$ That didn't even once show "Hello World" for me. \$\endgroup\$Paŭlo Ebermann– Paŭlo Ebermann2014年06月01日 00:13:41 +00:00Commented Jun 1, 2014 at 0:13
-
1\$\begingroup\$ @StéphaneGourichon: Yes, it remembers IPs, and there is no way to remove an IP before automatic removal. I decided to put the source code, so that you can check what it does. \$\endgroup\$null– null2016年01月07日 15:46:24 +00:00Commented Jan 7, 2016 at 15:46
-
1\$\begingroup\$ @StéphaneGourichon:
pg_query_params
pretty much protects from SQL injection. It won't be saved however, as'; DROP TABLE users;--
is not a valid IP (inet
only accepts IPs), and therefore if you have a fakeX-Forwarded-For
header, your visit won't be saved. I don't consider it a problem myself. \$\endgroup\$null– null2016年01月08日 17:07:55 +00:00Commented Jan 8, 2016 at 17:07 -
3\$\begingroup\$ I believe that this is a violation of a standard loophole: Outsourcing the real answer. \$\endgroup\$Joseph– Joseph2018年01月05日 22:40:44 +00:00Commented Jan 5, 2018 at 22:40
GW-BASIC, 20 bytes
1 COLOR 3
2 SCREEN 1
This is a fun one. COLOR 3
sets the foreground color to cyan. SCREEN 1
sets the output screen to one that does not have color. Therefore, you can run the program once, but if you try to run it again:
PHP, (削除) 22 (削除ここまで) 20 chars
<?=unlink(__FILE__);
EDIT: Removed two chars at end per comments.
-
2\$\begingroup\$ This can be further simplified as:
<?unlink(__FILE__);
, the trailing?>
is not required \$\endgroup\$leepowers– leepowers2014年06月08日 03:54:56 +00:00Commented Jun 8, 2014 at 3:54 -
\$\begingroup\$ You have to start with
<?=
or the start tag is not recognized. \$\endgroup\$Michael Mior– Michael Mior2014年06月08日 12:13:44 +00:00Commented Jun 8, 2014 at 12:13 -
2\$\begingroup\$ PHP destroying itself...ha! E_NOTICE line 3048 \$\endgroup\$metadings– metadings2014年06月08日 15:46:52 +00:00Commented Jun 8, 2014 at 15:46
-
\$\begingroup\$
<?
+space is also ok. You just cannot use a letter after the short tag. \$\endgroup\$Titus– Titus2017年01月17日 02:56:32 +00:00Commented Jan 17, 2017 at 2:56 -
1\$\begingroup\$ You can save one more byte by naming the file. <?=unlink("a.php"); \$\endgroup\$SlamJammington– SlamJammington2021年06月22日 17:14:08 +00:00Commented Jun 22, 2021 at 17:14
Ruby,14
It rewrites the source code as the name of the program.
IO.write 0,ドル0ドル
-
4\$\begingroup\$ And what if the program is named "IO.write 0,ドル0ドル"? "Assumption: Name the program something that isn't valid code in any language." \$\endgroup\$Brilliand– Brilliand2014年05月30日 21:57:50 +00:00Commented May 30, 2014 at 21:57
-
1\$\begingroup\$ @Brilland a ruby program should always end with
.rb
and even if its name was "IO.write 0,ドル 0ドル#.rb" that won't work because when writing it will include the full path. \$\endgroup\$Mohammad– Mohammad2014年06月01日 09:28:10 +00:00Commented Jun 1, 2014 at 9:28 -
5\$\begingroup\$ @Mhmd a ruby program can end with anything. For example
gem
andirb
are Ruby programs with no extension. \$\endgroup\$bfontaine– bfontaine2014年06月08日 09:13:08 +00:00Commented Jun 8, 2014 at 9:13 -
2\$\begingroup\$ @wizzwizz4 Shebangs have nothing to do with the extension.
ruby foo
andruby foo.rb
will work the same with or without a shebang../foo
and./foo.rb
(assuming they’re executable) will both fail without shebang and succeed with it (assuming correct code, etc). \$\endgroup\$bfontaine– bfontaine2016年05月06日 07:52:46 +00:00Commented May 6, 2016 at 7:52 -
1\$\begingroup\$ @bfontaine That they can doesn't mean they should. If they have shebangs it's clear what they will run in: an extension should be used if not. (An extension should always be used, but it isn't as important if you have a shebang.) \$\endgroup\$wizzwizz4– wizzwizz42016年05月06日 16:06:52 +00:00Commented May 6, 2016 at 16:06
NodeJS - 33 bytes
require('fs').unlink(__filename);
-
\$\begingroup\$ Could you describe what this does for those who are unfamiliar with the language? \$\endgroup\$Kyle Kanos– Kyle Kanos2014年05月30日 17:38:05 +00:00Commented May 30, 2014 at 17:38
-
1\$\begingroup\$ @KyleKanos It gets the filesystem module
fs
and the current filename__filename
and removes that filename from the filesystem. \$\endgroup\$Keen– Keen2014年05月30日 19:06:36 +00:00Commented May 30, 2014 at 19:06 -
9\$\begingroup\$ You can elide the semicolon because they are optional in JavaScript. \$\endgroup\$nalply– nalply2014年06月01日 12:07:55 +00:00Commented Jun 1, 2014 at 12:07
Bash (削除) 9 (削除ここまで) 8 Characters
nc -l 1&
Will only run once, because the port is blocked forever.
-
2\$\begingroup\$ I think this doesn't work, because
0
is a special port (it's reserved). When I run this on my computer I can open it multiple times. Also, it's a port< 1024
, which means it requires root access to be listened on. \$\endgroup\$heinrich5991– heinrich59912014年05月29日 21:22:48 +00:00Commented May 29, 2014 at 21:22 -
\$\begingroup\$
nc -l 1&
would fix the problem with0
and be one byte shorter, since the last space is not needed. \$\endgroup\$kasperd– kasperd2014年06月01日 14:59:50 +00:00Commented Jun 1, 2014 at 14:59 -
\$\begingroup\$ @kasperd Thanks for the input :-) \$\endgroup\$Falco– Falco2014年06月02日 07:40:44 +00:00Commented Jun 2, 2014 at 7:40
-
\$\begingroup\$ @heinrich5991 ok - than I would need 11 chars without root access to use a Port like 9654 \$\endgroup\$Falco– Falco2014年06月02日 07:41:32 +00:00Commented Jun 2, 2014 at 7:41
-
\$\begingroup\$ Care to explain why you downvoted ? \$\endgroup\$Falco– Falco2014年06月22日 22:57:04 +00:00Commented Jun 22, 2014 at 22:57
Batch (6 characters)
This is assuming we have permission to delete the file, and of course can be done on Windows (maybe MS-DOS as well).
del %0
Batch, 7 chars
Save unsaved work before trying this.
Assumes either a 64-bit system, or a 32-bit system with <2 GB RAM memory. Must be saved to a .bat or .cmd file before being launched, just entering it in cmd.exe won't work.
^ nul<^
It consumes a full CPU core and fills your RAM memory at a rate of several hundred megabytes per second. If you don't stop it in time (Task Manager), it will consume every byte of RAM on your computer, your entire system will freeze and you can't do anything before you forcibly shut down and restart your computer by means of cutting power or holding the power button down.
All credits go to txtechhelp on StackOverflow for this, see more information here: https://stackoverflow.com/q/23284131/1955334
-
\$\begingroup\$ How would this be possible ? What about paging ? Process priority ? \$\endgroup\$TheMaskedCucumber– TheMaskedCucumber2014年06月03日 18:17:59 +00:00Commented Jun 3, 2014 at 18:17
-
\$\begingroup\$ I ran this script, nothing happened. \$\endgroup\$Pavel– Pavel2016年12月01日 00:49:11 +00:00Commented Dec 1, 2016 at 0:49
-
\$\begingroup\$ @Pavel I'd guess it's fixed in Windows 10 then. I cannot reproduce it on Windows 10, but it works on Windows 8.1. \$\endgroup\$bilde2910– bilde29102016年12月11日 22:03:49 +00:00Commented Dec 11, 2016 at 22:03
-
1\$\begingroup\$ "It consumes a full CPU core and fills your RAM memory at a rate of several hundred megabytes per second. If you don't stop it in time (Task Manager), it will consume every byte of RAM on your computer, your entire system will freeze and you can't do anything before you forcibly shut down and restart your computer by means of cutting power or holding the power button down." Sounds like a horror story for computers... \$\endgroup\$noɥʇʎԀʎzɐɹƆ– noɥʇʎԀʎzɐɹƆ2016年12月31日 17:38:28 +00:00Commented Dec 31, 2016 at 17:38
Sinclair BASIC
10 POKE 23635,255
Moves the address of the BASIC program in memory away from where it should be (203) so the wrong data is found by the interpreter.
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!
-
2\$\begingroup\$ Funny way to not answer the challenge. +1 for hack value. \$\endgroup\$Stéphane Gourichon– Stéphane Gourichon2016年01月09日 06:34:51 +00:00Commented Jan 9, 2016 at 6:34
-
1\$\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– Stéphane Gourichon2016年01月09日 06:35:52 +00:00Commented Jan 9, 2016 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– Ian D. Scott2016年01月09日 16:11:29 +00:00Commented Jan 9, 2016 at 16:11
vi
in a single terminal environment has this effect, no escape from it unless you reboot :) Just a joke here. \$\endgroup\$echo "If you try to execute me again, it means you are an idiot.";
<-- Nobody will execute more than once :P \$\endgroup\$