Shut down the computer!

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!

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

Mathematica on Windows 16 Bytes

Invoke the shell command with Run.

Run@"Shutdown/p"

• Does this work on every OS, or a specific one? – Greg Martin Jan 24 '17 at 18:18
• I'm sure a Linux person of any gender could! Perhaps "Mathematica on Windows" is more precise in the post itself? – Greg Martin Jan 24 '17 at 20:20
• Lol. I use "guy" in the gender-neutral sense. slate.com/blogs/lexicon_valley/2016/02/10/… – Kelly Lowder Jan 24 '17 at 22:56
• 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. – Greg Martin Jan 25 '17 at 1:04
• 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). – user62131 Jan 25 '17 at 6:08

Bash 15 bytes

shutdown -h now


alternatively : init 0

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

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.

PHP, 20 bytes

shell_exec("init 0");


R, 20 19 bytes

shell("shutdown/s")


system("shutdown/s")

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

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

WIN/P


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

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

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.

Lisp, 38 bytes

(trivial-shell:shell-command "init 0")

• Which dialect of Lisp does this work in? – ckjbgames Feb 13 '17 at 16:08

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.

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

EthOS Shell - 1 byte

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

r

JScript, 53 bytes

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


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

VBScript, 46 bytes

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


Run with wscript scriptname.js.

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.

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

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.

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).

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.

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

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

halt


Requires you to be root.

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


shortC, 10 bytes

A$shutdown  Explanation/ungolfed: A main function$          execute system command
shutdown  shutdown


AppleScript (45 bytes)

tell application "System Events" to shut down

• That's considerably more verbose than telling app "Finder" to shut down. – Mark Sep 11 '18 at 0:57

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.

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.

• 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) – wizzwizz4 Jan 24 '17 at 19:09
• Do you really have to use poweroff? Wouldn't init 0 or halt -p work? – statox Jan 25 '17 at 8:28
• @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. :) – Robert Benson Jan 25 '17 at 15:05
• @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 :) – statox Jan 25 '17 at 15:35
• @RobertBenson I didn't know about these differences of rights, that's good to know! – statox Jan 25 '17 at 17:03

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


AutoIt (windows), 11 bytes:

Shutdown(1)


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.

• 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. – Ad Hoc Garf Hunter Jan 29 '17 at 23:34

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.

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


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.

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.

05AB1E (legacy) prior to commit June 4th, 2018, 10 bytes

’À…„‹/p’.E


Try it online without the .E.

Explanation:

In the commit of June 4th, 2018, the builtin .E has been changed from run_as_Bash_code to run_as_Python_code. So this only works prior to this commit.

’À…„‹/p’    # Push dictionary string "shutdown/p"
.E  # Run it as Bash code


05AB1E, 25 bytes

’£Ø os;os.‚«('À…„‹/p')’.E


Try it online without the .E.

Explanation:

The .E builtin is eval_in_Python in the current version of 05AB1E.

’£Ø os;os.‚«('À…„‹/p')’    # Push dictionary string "import os;os.system('shutdown/p')"
.E  # Evaluate as Python code


See this 05AB1E tips of mine (section How to use the dictionary?) to understand why ’À…„‹/p’ is "shutdown/p" and ’£Ø os;os.‚«('À…„‹/p')’ is "import os;os.system('shutdown/p')".

Note: Both work only on Windows. Could be modified to also work on other OS, i.e. "init 0".E and ’£Ø os;os.‚«('„‡ƒŸ')’.E for Linux.