83
\$\begingroup\$

Challenge:

In the programming language of your choice, shut down the machine that your code was executed on.

Rules

  • No shutting down by resource exhaustion (e.g.: forkbomb to force shutdown)
  • You are allowed to write code that only works in a specific environment/OS, if you wish.
  • Standard loopholes are forbidden

This is , thus the lowest amount of bytes wins!

\$\endgroup\$
  • 62
    \$\begingroup\$ I got friends, who do this in school and think they are 'hacking' :D \$\endgroup\$ – RaisingAgent Jan 24 '17 at 14:00
  • 12
    \$\begingroup\$ We should come up with some rule once to stop such `whatever`; Bash/Perl/PHP/Ruby/etc. stupiglots. \$\endgroup\$ – manatwork Jan 24 '17 at 14:22
  • 260
    \$\begingroup\$ Windows, 0 bytes. Leave the computer on for a few days and let automatic updates do their work \$\endgroup\$ – Luis Mendo Jan 24 '17 at 15:17
  • 28
    \$\begingroup\$ Anybody else notice the lack of TIO-links? \$\endgroup\$ – steenbergh Jan 24 '17 at 16:37
  • 8
    \$\begingroup\$ @MontyHarder After reading that page I have decided to coin the term shatdown: past tense of the verb shutdown \$\endgroup\$ – FGreg Jan 24 '17 at 17:34

57 Answers 57

2
\$\begingroup\$

Mathematica on Windows 16 Bytes

Invoke the shell command with Run.

Run@"Shutdown/p"
\$\endgroup\$
  • \$\begingroup\$ Does this work on every OS, or a specific one? \$\endgroup\$ – Greg Martin Jan 24 '17 at 18:18
  • 1
    \$\begingroup\$ I'm sure a Linux person of any gender could! Perhaps "Mathematica on Windows" is more precise in the post itself? \$\endgroup\$ – Greg Martin Jan 24 '17 at 20:20
  • 3
    \$\begingroup\$ Lol. I use "guy" in the gender-neutral sense. slate.com/blogs/lexicon_valley/2016/02/10/… \$\endgroup\$ – Kelly Lowder Jan 24 '17 at 22:56
  • 1
    \$\begingroup\$ Although this term is still being debated, it is certainly the case that "guys" is not uniformly gender-neutral; and research definitely shows that gendered language has a harmful effect on STEM disciplines and the women participating in them. I believe it's important to bend over backwards to be inclusive with our language, since the gender equity problem is not solving itself. \$\endgroup\$ – Greg Martin Jan 25 '17 at 1:04
  • 1
    \$\begingroup\$ Linux shutdown code: shutdown +0 is the traditional way to do a shutdown and power off (using the tersest representations of the flags); shutdown with no arguments will delay 60 seconds then shut down. On recent systemd-based systems you're supposed to use systemctl halt or systemctl poweroff instead, but that's more bytes so who cares about that. Note that any of these commands require root permissions (as shutting down the system could cause data loss for anyone else logged in at the time, it's an action that affects other users, so you need enough permissions to be able to do that). \$\endgroup\$ – user62131 Jan 25 '17 at 6:08
2
\$\begingroup\$

Bash 15 bytes

shutdown -h now

alternatively : init 0

\$\endgroup\$
  • 1
    \$\begingroup\$ useless use of sudo \$\endgroup\$ – Ipor Sircer Jan 24 '17 at 22:03
  • \$\begingroup\$ you're right given that only one user is logged in! :) \$\endgroup\$ – Abel Tom Jan 25 '17 at 6:25
2
\$\begingroup\$

TI-Basic, 3 bytes

ClrHome:"

Basically, this displays a blank screen and then waits for automatic timeout to kick in and shutdown the calculator, although you will not notice when this happens. The blank screen resembles a shutdown so you will not notice when the device actually shuts down.

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

R, 20 19 bytes

shell("shutdown/s")

Previous answer :

system("shutdown/s")
\$\endgroup\$
  • 1
    \$\begingroup\$ shell("shutdown/s")? (19 bytes) \$\endgroup\$ – djhurio Jan 25 '17 at 14:33
  • \$\begingroup\$ @djhurio well done ! Thanks ! \$\endgroup\$ – Frédéric Jan 25 '17 at 17:37
2
\$\begingroup\$

DOS 7.1 (Windows 98) command prompt/batch, 5

WIN/P

Only works from real mode (MS-DOS command prompt).

\$\endgroup\$
  • \$\begingroup\$ Isn't the switch for soft-power-off /Z? \$\endgroup\$ – MrPaulch Jan 27 '17 at 12:28
  • \$\begingroup\$ Might be. It's been a long time. \$\endgroup\$ – Joshua Jan 27 '17 at 16:18
2
\$\begingroup\$

Assembly (Z80, Amstrad CPC), one byte

Amstrad CPC cannot be physically shut down by software, but system can be reset.

The following one-byte assembly program is enough to reset system:

C7    ;RST 0

It is a valid assembly program (in RAM) since no header of any kind is needed on this system.

You can install this program from BASIC e.g.:

poke 999,&c7

Then call it:

call 999

Can I haz test?

You can test at once on CPCBox - Amstrad CPC emulator in Javascript.

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

Lisp, 38 bytes

(trivial-shell:shell-command "init 0")
\$\endgroup\$
  • \$\begingroup\$ Which dialect of Lisp does this work in? \$\endgroup\$ – ckjbgames Feb 13 '17 at 16:08
2
\$\begingroup\$

Alternative Python on Linux + SysRQ, 47 42 bytes:

open("/proc/sysrq-trigger","w").write("o")

Can be run either from python -c or from script, and requires root privileges to write to the file. The idea isn't python-specific. Writing to that file on Linux can be done in any other way - bash, perl, ruby , etc.

Note that this isn't a graceful shutdown, so expect that there is potential for data to be lost. See the Wikipedia article for more info on sysrq keys.

\$\endgroup\$
  • 1
    \$\begingroup\$ open("/proc/sysrq-trigger","w").write("o") saves a few bytes. \$\endgroup\$ – Dennis Jan 28 '17 at 1:31
2
\$\begingroup\$

EthOS Shell - 1 byte

Just type the letter r to reboot, i.e.:

r

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

JScript, 53 bytes

new ActiveXObject('WScript.Shell').Run("shutdown /s")

JScript is Microsoft's implementation of JavaScript. Run with wscript scriptname.js.

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

VBScript, 46 bytes

CreateObject("WScript.Shell").Run "shutdown /s"

Run with wscript scriptname.js.

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

Python 3, 53 bytes

import subprocess
subprocess.call(["shutdown", "/s"])

This does give a window and shutdown warning. If you wanted to shut it down immediately then you would use "/p".

You could also call the function from the USER library but this is a longer solution than the submitted version.

\$\endgroup\$
  • \$\begingroup\$ Would os.system('shutdown\s') not work? I don't want to try it right now in case it does work :) \$\endgroup\$ – Farhan.K Jan 30 '17 at 11:16
  • \$\begingroup\$ @Farhan.K No. It tries to find s inside the shutdown directory. (Windows 10) \$\endgroup\$ – wizzwizz4 Feb 17 '17 at 19:04
  • 1
    \$\begingroup\$ @wizzwizz4 Sorry I meant os.system('shutdown/s') \$\endgroup\$ – Farhan.K Feb 20 '17 at 9:39
1
\$\begingroup\$

PHP, 20 bytes

shell_exec("init 0");
\$\endgroup\$
1
\$\begingroup\$

Amstrad CPC Basic, 7 keypresses.

Amstrad CPC cannot be physically shut down by software, but system can be reset with this BASIC call:

call 0

How it works

General info

At the beginning of memory (either RAM or ROM, both are prepared for this), there is a table for Z80 RST instruction. These instructions, related to Z80 interrupt handling, are also used (even on machines other than Amstrad CPC, like TI-81) for other tricks because one-byte long assembly instruction is enough to reach any of the 8 entries.

On the Amstrad CPC they are used to space-efficiently call code in ROM, RAM, expansion RAM/ROM, see BIOS Function Summary - CPCWiki or ref page 8+. This is used extensively to make RAM-based indirections that allow easily calling ROM routines from RAM (even from BASIC, like call &bd19 that waits for screen refresh) and also, by changing them, modifying system behavior even though system is in ROM (like changing how text is displayed, or even redirecting it to printer by changing two bytes of RAM). See BB00 and following in BIOS Function Summary - CPCWiki or Firmware Guide

Specific use

Entry zero jumps to a ROM-base routine that performs full machine reset. The simplest way to call it from BASIC it call 0.

Variant

If you insist on a program that can be run rather than a direct command, it takes 8 keypresses:

1call 0

Then you can run.

Can I haz test?

You can test at once on CPCBox - Amstrad CPC emulator in Javascript.

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

Commodore 128 (from native mode), 6 characters including user response and return

GO64

You will then be prompted with:

ARE YOU SURE? 

Press Y and enter. This shuts down the C128 native mode (I know it doesn't power off the computer, but that's as close as I can get).

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

Machine code (x86 boot loader), 2 bytes

fa f4

Disassembly

cli ; disable all maskable interrupts
hlt ; stop the processor until an interrupt or hard reset happens

The HLT instruction causes the CPU to stop execution and enter low-power mode, until any interrupt (maskable or NMI) or hard reset happens. This doesn't power off the whole system, but the original IBM PC didn't have any way to power off without the user mechanically pressing the switch. Also, if there happens an NMI, and its handler returns, the program will resume and execute arbitrary code which happens after f4. This is unlikely though, since NMI usually signals about hardware faults.

This same code will work as a DOS .COM program.

\$\endgroup\$
  • \$\begingroup\$ IIRC, this could be used to hang Win9x, where the DOS box had an IOPL of 3 \$\endgroup\$ – ninjalj Jul 15 '17 at 16:17
1
\$\begingroup\$

Perl 5 (on a unix-like OS), 6 bytes

`halt`

Requires you to be root.

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

Jelly, 25 bytes

It makes use of the undocumented monad ŒV, which takes a list, concatenates it and evaluates it using Python's eval.

“WƒuUḤøMŒƁEḄV#ẆṄⱮṁƬṅȯV»ŒV

The compressed string translates to import os;os.system("poweroff") so it only works on Linux (with root). The following code was used for compressing.

Compress().dictionary('import')
          .string(' os;os.')
          .dictionary('system')
          .string('("')
          .dictionary('power')
          .dictionary('off')
          .string('")').go()

By changing the poweroff it can be extended to other systems (but power and off are in the dictionary so this will probably be the shortest). For example, following 33-byte version worked on a Mac.

“¡Ṁ\\meṾṭ£ȧ+⁾⁸}ḶṠȯƇọṣỴ\\Ḃ⁴8ġṢṾ»ŒV
\$\endgroup\$
1
\$\begingroup\$

shortC, 10 bytes

A$shutdown

Explanation/ungolfed:

A           main function
 $          execute system command
  shutdown  shutdown
\$\endgroup\$
1
\$\begingroup\$

AppleScript (45 bytes)

tell application "System Events" to shut down
\$\endgroup\$
  • \$\begingroup\$ That's considerably more verbose than telling app "Finder" to shut down. \$\endgroup\$ – Mark Sep 11 '18 at 0:57
0
\$\begingroup\$

AWK, 39 bytes

#!/bin/awk -f
BEGIN{"poweroff"|getline}

Assumes running on Linux and that AWK is located at /bin/awk and that the poweroff command is in the users path.

Since it uses the BEGIN block, no input is necessary. It simply pipes the output of the poweroff command into getline which it precedes to ignore.

Should work for Windows users provided they are in an environment similar to Cygwin.

\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need to provide the shebang; just add 3 bytes for the flag in your title (something like ### AWK, 39 bytes + 3 (` -f`) \$\endgroup\$ – wizzwizz4 Jan 24 '17 at 19:09
  • \$\begingroup\$ Do you really have to use poweroff? Wouldn't init 0 or halt -p work? \$\endgroup\$ – statox Jan 25 '17 at 8:28
  • \$\begingroup\$ @wizzwizz4 The request was for a program, so to make a standalone program requires the #! and the -f. If I just wanted a script I would leave out that line entirely since: awk 'BEGIN{"poweroff"|getline}' works just fine. That would save me 13 bytes or so, but I wouldn't consider it a program. Just me being pedantic I guess. :) \$\endgroup\$ – Robert Benson Jan 25 '17 at 15:05
  • 1
    \$\begingroup\$ @RobertBenson I can't test it right now but given the bash answer I think you can save some bits. Indeed that's nice to have answer with awk that's not the first language which comes in mind when we think about codegolf :) \$\endgroup\$ – statox Jan 25 '17 at 15:35
  • 1
    \$\begingroup\$ @RobertBenson I didn't know about these differences of rights, that's good to know! \$\endgroup\$ – statox Jan 25 '17 at 17:03
0
\$\begingroup\$

C, macOS, 18 bytes

main(){reboot(8);}

This is not a proper shutdown. Usual caveats apply. On my MacBook Air, this resulted in a black screen with a spinning indicator, then a shutdown after a few seconds.

x64 Assembly, macOS, 11 bytes (untested)

From disassembling the reboot function in libSystem:

movl    $0x2000037, %eax   # b8 37 00 00 02
pushq   $0x08              # 6a 08
popq    %r10               # 41 5a
syscall                    # 0f 05
\$\endgroup\$
0
\$\begingroup\$

AutoIt (windows), 11 bytes:

Shutdown(1)
\$\endgroup\$
0
\$\begingroup\$

Python, 53 bytes

import subprocess
subprocess.call(["shutdown", "/s"])

Pretty simple code but can do some damage if you haven't saved anything.

Breakdown

import subprocess

This imports the subprocess module that comes with Python that can be used across the script.

subprocess.call(["shutdown", "/s"])

subprocess.call runs a process as if you pressed Win + R. It requires a list of strings that will be joined with a space between them and executes it on the PC's shell.

\$\endgroup\$
  • 1
    \$\begingroup\$ Instead of import subprocess you could do from subprocess import* this way you don't spend as many bytes on the next line camping the subprocess module. \$\endgroup\$ – Sriotchilism O'Zaic Jan 29 '17 at 23:34
0
\$\begingroup\$

Perl 5.x (on CentOS Linux), 27 bytes

system("shutdown -h now");

Make system call to shutdown in Linux to halt (-h) now.
Changing -h to -r will shutdown then reboot Linux.

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

VBA (32-Bit, Windows Only), 72 Bytes

An anonymous VBE immediate function that calls the declared windows function ExitWindowsEx from user32.dll.

To be clear, this requires a 32-Bit version of MS Office, not a 32-Bit version of Windows

Declared Function

Declare Sub ExitWindowsEx Lib"user32"(ByVal A&,ByVal B&)

Anonymous VBE immediate window function

ExitWindowsEx 1,0
\$\endgroup\$
0
\$\begingroup\$

Burlesque - 22 bytes

"cmd"{"shutdown /s"}ex

Obviously you can not try this online.

On linux you can probably get away with "shutdown"ex if your distro offers the shutdown util.

\$\endgroup\$

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.