88
\$\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!

Stewie Griffin
46.8k16 gold badges137 silver badges304 bronze badges
asked Jan 24, 2017 at 13:38
\$\endgroup\$
33
  • 68
    \$\begingroup\$ I got friends, who do this in school and think they are 'hacking' :D \$\endgroup\$ Commented Jan 24, 2017 at 14:00
  • 13
    \$\begingroup\$ We should come up with some rule once to stop such `whatever`; Bash/Perl/PHP/Ruby/etc. stupiglots. \$\endgroup\$ Commented Jan 24, 2017 at 14:22
  • 278
    \$\begingroup\$ Windows, 0 bytes. Leave the computer on for a few days and let automatic updates do their work \$\endgroup\$ Commented Jan 24, 2017 at 15:17
  • 29
    \$\begingroup\$ Anybody else notice the lack of TIO-links? \$\endgroup\$ Commented Jan 24, 2017 at 16:37
  • 10
    \$\begingroup\$ @MontyHarder After reading that page I have decided to coin the term shatdown: past tense of the verb shutdown \$\endgroup\$ Commented Jan 24, 2017 at 17:34

61 Answers 61

2
\$\begingroup\$

C++, (削除) 48 (削除ここまで) 47 (Windows) (削除) 46 (削除ここまで) 45 (Linux) bytes

Removed 1 byte thanks to Snowman

#include<cstdlib>
main(){system("shutdown/p");}

This should shutdown the system for windows

#include<cstdlib>
main(){system("poweroff");}

This should shutdown the system for Linux

answered Jan 24, 2017 at 14:12
\$\endgroup\$
8
  • \$\begingroup\$ do you need the ;? \$\endgroup\$ Commented Jan 24, 2017 at 14:12
  • 1
    \$\begingroup\$ @LliwTelracs Pretty sure it is "shutdown /p" and not "-p" and the space is not needed. \$\endgroup\$ Commented Jan 24, 2017 at 14:14
  • \$\begingroup\$ The semicolon is required for proper c++ syntax, using a slash allows for a shorter answer for windows but makes it no longer work for linux. Although I added the wrong character for shutdown that is shared between windows and linux \$\endgroup\$ Commented Jan 24, 2017 at 14:21
  • \$\begingroup\$ @LliwTelracs Although you can do whatever you want, I suggest abusing the 2nd rule and not caring about Linux functionality, since this is a code-golf and low amount of bytes is better. \$\endgroup\$ Commented Jan 24, 2017 at 14:25
  • 1
    \$\begingroup\$ #include<cstdlib> will shave off a byte. \$\endgroup\$ Commented Jan 24, 2017 at 18:14
2
\$\begingroup\$

Mathematica on Windows 16 Bytes

Invoke the shell command with Run.

Run@"Shutdown/p"
answered Jan 24, 2017 at 16:28
\$\endgroup\$
6
  • \$\begingroup\$ Does this work on every OS, or a specific one? \$\endgroup\$ Commented Jan 24, 2017 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\$ Commented Jan 24, 2017 at 20:20
  • 3
    \$\begingroup\$ Lol. I use "guy" in the gender-neutral sense. slate.com/blogs/lexicon_valley/2016/02/10/… \$\endgroup\$ Commented Jan 24, 2017 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\$ Commented Jan 25, 2017 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\$ Commented Jan 25, 2017 at 6:08
2
\$\begingroup\$

Bash 15 bytes

shutdown -h now

alternatively : init 0

answered Jan 24, 2017 at 16:20
\$\endgroup\$
2
  • 1
    \$\begingroup\$ useless use of sudo \$\endgroup\$ Commented Jan 24, 2017 at 22:03
  • \$\begingroup\$ you're right given that only one user is logged in! :) \$\endgroup\$ Commented Jan 25, 2017 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.

answered Jan 25, 2017 at 13:28
\$\endgroup\$
2
\$\begingroup\$

PHP, 20 bytes

shell_exec("init 0");
answered Jan 25, 2017 at 14:59
\$\endgroup\$
2
\$\begingroup\$

R, (削除) 20 (削除ここまで) 19 bytes

shell("shutdown/s")

Previous answer :

system("shutdown/s")
answered Jan 24, 2017 at 21:29
\$\endgroup\$
2
  • 1
    \$\begingroup\$ shell("shutdown/s")? (19 bytes) \$\endgroup\$ Commented Jan 25, 2017 at 14:33
  • \$\begingroup\$ @djhurio well done ! Thanks ! \$\endgroup\$ Commented Jan 25, 2017 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).

answered Jan 25, 2017 at 23:19
\$\endgroup\$
2
  • \$\begingroup\$ Isn't the switch for soft-power-off /Z? \$\endgroup\$ Commented Jan 27, 2017 at 12:28
  • \$\begingroup\$ Might be. It's been a long time. \$\endgroup\$ Commented Jan 27, 2017 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.

answered Jan 26, 2017 at 15:13
\$\endgroup\$
1
2
\$\begingroup\$

Lisp, 38 bytes

(trivial-shell:shell-command "init 0")
\$\endgroup\$
1
  • \$\begingroup\$ Which dialect of Lisp does this work in? \$\endgroup\$ Commented Feb 13, 2017 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.

answered Jan 28, 2017 at 1:18
\$\endgroup\$
1
  • 1
    \$\begingroup\$ open("/proc/sysrq-trigger","w").write("o") saves a few bytes. \$\endgroup\$ Commented Jan 28, 2017 at 1:31
1
\$\begingroup\$

JScript, 53 bytes

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

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

answered Jan 25, 2017 at 13:19
\$\endgroup\$
1
\$\begingroup\$

VBScript, 46 bytes

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

Run with wscript scriptname.js.

answered Jan 25, 2017 at 13:24
\$\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\$
3
  • \$\begingroup\$ Would os.system('shutdown\s') not work? I don't want to try it right now in case it does work :) \$\endgroup\$ Commented Jan 30, 2017 at 11:16
  • \$\begingroup\$ @Farhan.K No. It tries to find s inside the shutdown directory. (Windows 10) \$\endgroup\$ Commented Feb 17, 2017 at 19:04
  • 1
    \$\begingroup\$ @wizzwizz4 Sorry I meant os.system('shutdown/s') \$\endgroup\$ Commented Feb 20, 2017 at 9:39
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).

answered Jan 27, 2017 at 15:10
\$\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.

answered Jan 28, 2017 at 6:59
\$\endgroup\$
1
  • \$\begingroup\$ IIRC, this could be used to hang Win9x, where the DOS box had an IOPL of 3 \$\endgroup\$ Commented Jul 15, 2017 at 16:17
1
\$\begingroup\$

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

`halt`

Requires you to be root.

answered Jan 29, 2017 at 6:31
\$\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}ḶṠȯƇọṣỴ\\Ḃ48ġṢṾ»ŒV
answered Feb 17, 2017 at 13:37
\$\endgroup\$
1
\$\begingroup\$

shortC, 10 bytes

A$shutdown

Explanation/ungolfed:

A main function
 $ execute system command
 shutdown shutdown
answered Jun 6, 2017 at 4:36
\$\endgroup\$
1
\$\begingroup\$

AppleScript (45 bytes)

tell application "System Events" to shut down
Taylor Raine
8,9732 gold badges29 silver badges53 bronze badges
answered Jun 6, 2017 at 15:43
\$\endgroup\$
1
  • \$\begingroup\$ That's considerably more verbose than telling app "Finder" to shut down. \$\endgroup\$ Commented Sep 11, 2018 at 0:57
1
\$\begingroup\$

UEFI Shell - 16 bytes

reset -s

In ASCII it would be 8 bytes but since UEFI uses UCS-16, every character takes up 2 bytes.

answered Feb 18, 2020 at 12:23
\$\endgroup\$
1
\$\begingroup\$

Vimscript (Linux Bash) 8 bytes

:!init 0

Vimscript (Windows CMD) 12 bytes

:!shutdown/p

Vimscript (Windows PowerShell) 15 bytes

:!Stop-Computer

Two dots : are required for the vim command, and the '!' redirects the command to the shell runing behind. You may require some extra permissions for this to work.

answered Feb 18, 2020 at 19:06
\$\endgroup\$
1
\$\begingroup\$

ARM Thumb machine code, Nintendo DS, 30 bytes

05 48 05 49 00 23 02 88 12 0a fc d2 01 80 43 80
09 0c 40 23 f7 e7 c0 01 00 04 02 88 02 80

Thumb function. Runs at an address that is 2 byte aligned, 4 byte unaligned, on the ARM7. The ARM9 cannot access these registers.

This version follows the spec by writing the two halfwords separately, like the code in libnds. Known to work on DeSuMME (it will log it in the console menu) and melonDS, and should work on hardware.

Assembler source:

 .syntax unified
 .arch armv4t
 .thumb
 // Force misalignment to align PC load
 // Not part of function
 .p2align 2,0
 .hword 0
 // Begin code
 // void shutdown(void);
 .thumb_func
 .globl shutdown 
shutdown:
 // Load address of SPI register
 ldr r0, REG_SPICNT
 // Load packed SPI commands
 ldr r1, SPI_COMMANDS
 // Set SPIDATA to select PM_CONTROL_REG
 movs r3, #0x00
.Lloop:
 // Wait for REG_SPICNT & SPI_BUSY to clear
 ldrh r2, [r0]
 // Test bit 7 by shifting into carry flag
 lsrs r2, r2, #8
 bcs .Lloop
 // Store the low 16 bits of the command to REG_SPICNT
 strh r1, [r0]
 // Store data to REG_SPIDATA
 strh r3, [r0, #2]
 // Shift in the next SPI command
 lsrs r1, r1, #16
 // Set SPIDATA to PM_SYSTEM_PWR
 movs r3, #0x40
 // Run again with the new commands
 // Then spin again until the SPI powers off the system.
 b .Lloop
REG_SPICNT:
 // Address of ARM7 IO register
 .word 0x040001c0
 // Two packed halfwords
SPI_COMMANDS:
 // Start a command on the Power Management device,
 // read a byte to select the register
 // SPI_ENABLE | SPI_BAUD_1MHz | SPI_BYTE_MODE | SPI_CONTINUOUS | SPI_DEVICE_POWER
 .hword 0x8802
 // Finish the command on the Power Management device,
 // read a byte to run the action.
 // SPI_ENABLE | SPI_BAUD_1MHz | SPI_BYTE_MODE | SPI_DEVICE_POWER
 .hword 0x8002

ARM Thumb machine code function, Nintendo DS, unsafe, 28 bytes

03 48 04 49 02 88 12 0a fc d2 01 60 02 49 f9 e7 
c0 01 00 04 02 88 00 00 02 80 40 00

This function runs at a 4 byte aligned address on the ARM7.

This cheats 2 bytes by writing to SPICNT and SPIDATA at the same time in a full word write, something that has not yet been confirmed to work on hardware. It does not work on DeSuMME, but this might just be an emulator bug.

I am including this because it uses different code.

Assembler source:

 .syntax unified
 .arch armv4t
 .thumb
 // Force 4 byte alignment
 .p2align 2,0
 // Begin code
 .thumb_func
 .globl shutdown
shutdown:
 // address of SPI register
 ldr r0, REG_SPICNT
 // Set SPI register bank
 ldr r1, SPI_SELECT_PM_CONTROL_REG
.Lloop:
 // Wait for REG_SPICNT & SPI_BUSY to clear
 ldrh r2, [r0]
 // Test bit 7 by shifting into carry flag
 lsrs r2, #8
 bcs .Lloop
 // Store to both SPICNT and SPIDATA
 str r1, [r0]
 // Send SPI command
 ldr r1, SPI_SEND_PM_SYSTEM_PWR
 // Loop again with the new value.
 b .Lloop
 .p2align 2,0
REG_SPICNT:
 .word 0x040001c0
 // SPICNT = SPI_ENABLE | SPI_CONTINUOUS | SPI_BAUD_1MHz | SPI_DEVICE_POWER
 // SPIDATA = PM_CONTROL_REG
SPI_SELECT_PM_CONTROL_REG:
 // Combined halfwords
 .hword 0x8802
 .hword 0x0000
 // SPICNT = SPI_ENABLE | SPI_BAUD_1MHz | SPI_DEVICE_POWER
 // SPIDATA = PM_SYSTEM_PWR
SPI_SEND_PM_SYSTEM_PWR:
 // Combined halfwords
 .hword 0x8002
 .hword 0x0040
answered Oct 20, 2022 at 17:48
\$\endgroup\$
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.

answered Jan 24, 2017 at 16:13
\$\endgroup\$
8
  • 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\$ Commented Jan 24, 2017 at 19:09
  • \$\begingroup\$ Do you really have to use poweroff? Wouldn't init 0 or halt -p work? \$\endgroup\$ Commented Jan 25, 2017 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\$ Commented Jan 25, 2017 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\$ Commented Jan 25, 2017 at 15:35
  • 1
    \$\begingroup\$ @RobertBenson I didn't know about these differences of rights, that's good to know! \$\endgroup\$ Commented Jan 25, 2017 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 0ドルx2000037, %eax # b8 37 00 00 02
pushq 0ドルx08 # 6a 08
popq %r10 # 41 5a
syscall # 0f 05
answered Jan 27, 2017 at 15:33
\$\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.

answered Jan 29, 2017 at 23:31
\$\endgroup\$
1
  • 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\$ Commented Jan 29, 2017 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
answered Oct 6, 2017 at 18:45
\$\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.

answered Nov 24, 2018 at 16:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.