Challenge:

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

Rules

  • No shutting down by resource exhaustion (eg: 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 shortest amount of bytes wins!

share|improve this question
21  
I got friends, who do this in school and think they are 'hacking' :D – RaisingAgent 17 hours ago
6  
We should come up with some rule once to stop such `whatever`; Bash/Perl/PHP/Ruby/etc. stupiglots. – manatwork 16 hours ago
69  
Windows, 0 bytes. Leave the computer on for a few days and let automatic updates do their work – Luis Mendo 15 hours ago
3  
@briantist Automatic updates have at times crashed my system, and I have mine set to not restart after a BSOD or other failures.....does that count? :P – NZKshatriya 15 hours ago
7  
Anybody else notice the lack of TIO-links? – steenbergh 14 hours ago

30 Answers 30

Batch, 10 bytes

On windows, this would suffice

shutdown/s

This will set up a shutdown in one minute.

Alternatively, as @flashbang points out:

shutdown/p

This shuts down the computer immediately.

share|improve this answer
27  
Press the big button on the front of your computer = 0 bytes – RaisingAgent 17 hours ago
38  
@RaisingAgent Here at PPCG we solve our problems in code. Physical labour is frowned upon. – steenbergh 17 hours ago
1  
Is it possible for a shorter code to exist written in assembly or parallel language(s) ? – Suhrid Mulay 16 hours ago
1  
@steenbergh I don't know... pressing one big button vs 10 keystrokes? I'll go with the button – WorldSEnder 10 hours ago

GRUB shell, 4 bytes

Golfed

halt

Command: halt --no-apm

The command halts the computer. If the --no-apm option is specified, no APM BIOS call is performed. Otherwise, the computer is shut down using APM.

share|improve this answer

PowerShell, 13 bytes

Stop-Computer

Pretty self-explanatory? Doesn't work on PowerShell core, so it won't work on PowerShell for Linux/MacOS (already tried it on TIO :-D ).

Bonus Garbage Submission, 12 bytes

gcm s*r|iex

In my testing, this works on Windows 2003 with PowerShell 2.0. It won't work on most of the newer versions, and it may not work depending on which modules are installed. It's quite terrible!

This searches the list of commands for any with the pattern s*r and then runs all of them! To be clear, this is dangerous, don't do this!

On my 2003 machine, the 3 commands it returns are:

CommandType     Name                                                Definition                                         
-----------     ----                                                ----------                                         
Application     scrnsave.scr                                        C:\WINDOWS\system32\scrnsave.scr                   
Application     ssmarque.scr                                        C:\WINDOWS\system32\ssmarque.scr                   
Cmdlet          Stop-Computer                                       Stop-Computer [[-ComputerName] <String[]>] [[-Cr...

Yes, it does launch the marquee screensaver, but no, it does not wait for it to complete before launching Stop-Computer.

This approach can work on newer machines, again depending on which modules are installed, but the best I could do on Windows 2012 and 2016 was 14 bytes:

gcm sto*er|iex

(and yeah, sure, the winbatch submission will work in PowerShell and it's shorter, but.. that's no fun)

share|improve this answer
    
Shorter one for Win7 & Win10 (and maybe more): gcm *p-c*|iex -- and there's only the one result too! – Bob 2 hours ago

MATLAB, 11 bytes

!shutdown/p

Somewhat similar to Stewie Griffin's approach. However, MATLAB has way shorter ways of invoking system commands; in this case, ! is used. Windows does not need .exe for command names, so that's left out as well. The / option is still supported (I'm running Windows 10), and negates the need for the space. Alternatives are:

system shutdown/p % 17 bytes; uses so-called 'command' syntax.
dos shutdown/p    % 14 bytes; the 'dos' command can be used just as well.
share|improve this answer

Matlab, 21 Bytes

system('shutdown -s')

I haven't tried this (closing all programs to do a restart in the middle of the day isn't really tempting), but it should work.. This works...

I'll add a short explanation later, my laptop is currently off.

share|improve this answer
27  
"I'll add a short explanation later, my laptop is currently off." <- The danger of a shutdown challenge – Suever 16 hours ago
1  
Axaxaxaxa, well, restarting your computer every once in a while can't be bad :) – RaisingAgent 16 hours ago
2  
I out-golfed you by 10 bytes... :) – Sanchises 16 hours ago
5  
You're wrong wrong wrooong! My code is shortest! I have the best code! My code is the greatest! And no, I'm not lying, I'm just providing some alternative facts! – Stewie Griffin 12 hours ago
1  
You are the most dishonest person. Really, this place is full of very, very dishonest people. Sad! My code is very small. It could fit into the palm of my hand. Very small. My code will make America great again. Mexico will pay for it. – Sanchises 9 hours ago

Bash, 7 6 bytes

init 0

Assuming that the program is run as root.

share|improve this answer
    
Indeed, powering off the machine is required. Thus you need 'halt -p' as a valid answer – P. Ktinos 16 hours ago
1  
This editing massacre :) – RaisingAgent 16 hours ago

Assembly (x64, Linux, gcc), 22 21 bytes

xor  %eax,        %eax
mov  $0x58,       %al
mov  $0xfee1dead, %ebx
mov  $0x28121969, %ecx
mov  $0x4321fedc, %edx
int  $0x80

Must be run as root. This is equivalent to pressing the power button.

To prevent file corruption, make sure you execute sync first.

Byte count

objdump shows the following generated byte code.

4004fd:       31 c0                   xor    %eax,%eax
4004ff:       b0 58                   mov    $0x58,%al
400501:       bb ad de e1 fe          mov    $0xfee1dead,%ebx
400506:       b9 69 19 12 28          mov    $0x28121969,%ecx
40050b:       ba dc fe 21 43          mov    $0x4321fedc,%edx
400510:       cd 80                   int    $0x80
share|improve this answer
5  
Apparently, 0x28121969 was chosen because 28.12.1969 is Linus Torvalds's birthday. – Dennis 9 hours ago

Commodore 64, 11 bytes

0 sys64738

This is from the BASIC prompt. Or...

Commodore 64, 6502 assembly, 3 bytes (assembled), 16 bytes (source)

*=8000
JMP $FCE2
share|improve this answer
    
I think that using the Commodore 64's direct input mode from BASIC with sys64738 will use zero bytes as there is no program or anything else to store; You are using screen RAM but that RAM will already be filled with spaces, so you're just replacing those spaces with text. – Shaun Bebbers 13 hours ago
2  
The source code takes up space. Just because it doesn't reserve space on the target machine doesn't mean it takes up 0 bytes. Otherwise the answer would be ``. – wizzwizz4 12 hours ago
    
I agree with @wizzwizz4 but even with the line number and space isn't your submission 10 bytes? Or are you counting the newline too? – briantist 12 hours ago
1  
You don't need the space between the 0 and the sys64738, you can replace the sys with the shortcut form sY, and I don't count the trailing newline in program length. However, I don't think your code meets the requirements: the code at 64738 does a soft reset, not a poweroff. – Mark 8 hours ago

SmileBASIC (3.3.2), 14 bytes

READ VAR(A)[0]

Triggers a crash causing the 3DS to restart. I hope this counts.

share|improve this answer
    
I thought this had been patched... Please include a version number. – wizzwizz4 11 hours ago
1  
It has been patched in version 3.5, which hasn't been released outside of Japan yet. – 12Me21 11 hours ago
    
@12Me21 Sure it counts. Nice choice of environment. – P. Ktinos 9 hours ago

Java (versions 8+), 101 98 bytes

Windows

interface C{static void main(String[]a)throws Exception{Runtime.getRuntime().exec("shutdown/s");}}
share|improve this answer
2  
It's shorter to replace class with interface as that allows you to omit public from main. – Pavel 15 hours ago
2  
You can use a method, since the question did not specify the submission must be a full program: void f()throws Exception{Runtime.getRuntime().exec("shutdown/s");} – Snowman 12 hours ago
    
Wouldn't the Linux version be a bit shorter? – jpmc26 5 hours ago

Python 2, 33 bytes

import os;os.system('shutdown/s')

This just might be the least interesting or helpful code I have ever written.

share|improve this answer
1  
I'm very lazy to shutdown windows and open my Linux system just to test halt -p or init 0. Can someone verify this :P – Gurupad Mamadapur 14 hours ago
    
Someone else posted the Linux shell version. – jpmc26 5 hours ago

C++, 51 bytes

#include<os>
void Service::start(){OS::shutdown();}

Only works in IncludeOS (v0.10.0-rc.1)

Test:

$ pwd
/home/simon/IncludeOS/examples/demo_service

$ cat service.cpp
#include<os>
void Service::start(){OS::shutdown();}

$ mkdir build && cd build && cmake .. && make && cd ..
[...]

$ boot build/IncludeOS_example
[...]
================================================================================
================================================================================
 IncludeOS v0.10.0-rc.1-dirty
 +--> Running [ IncludeOS minimal example ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$
share|improve this answer

Groovy, 23 bytes

'shutdown /s'.execute()
share|improve this answer
2  
Is the space before the slash necessary? – steenbergh 16 hours ago
2  
@steenbergh Yes, it is needed. – Gurupad Mamadapur 16 hours ago
    
@GurupadMamadapur are you sure? – TrojanByAccident 11 hours ago
    
@TrojanByAccident Yes, I tested it. – Gurupad Mamadapur 11 hours ago

AutoIt, 11 bytes

Shutdown(1)

Shutdown function gets combination of following codes

0 = Logoff
1 = Shutdown
2 = Reboot
4 = Force
8 = Power down
16= Force if hung
32= Standby
64= Hibernate

To shutdown and power down, for example, the code would be 9 (shutdown + power down = 1 + 8 = 9).

share|improve this answer
    
0 (logoff) is a special case here; it does not combine with Standby or Hibernate implicitly. – wizzwizz4 11 hours ago
    
OK 0 + 32 = 32 – rahnema1 11 hours ago

Clojure, 40 39 bytes

(.exec(Runtime/getRuntime)"shutdown/s")

-1 byte by changing the flag from -s to /s, which allowed me to get rid of the space.

Basically the Java answer.

Testing this was fun. Windows decided to do a firmware update on my Surface the second it shut down. Of course.

(defn shutdown []
  (.exec (Runtime/getRuntime) "shutdown -s"))
share|improve this answer

Spectrum Basic, 3 bytes

NEW 

(not sure how these are counted - 1 token)

share|improve this answer
2  
Welcome to the site! I'm pretty sure this would be counted as 3 bytes (although I don't know anything about spectrum basic). I know some languages treat each token as one byte (such as TI-BASIC), but I don't know if that's the case for spectrum basic. – DJMcMayhem 13 hours ago
3  
It was eons ago, I've last used the ZX Spectrum, but if I recall it right, NEW just resets the environment (i.e. program and variables), but does not actually shutdown the machine. – zeppelin 12 hours ago
    
Yep - IIRC, on the original ZX, or on the +2 in '48k mode', each standard command was typed with one key (maybe with a function key(s)) and I believe stored as a single byte. Typos were greatly reduced. But not sure of the rules here :) You could post it as the equivalent one-byte ascii code point and include the readable version! But comment on NEW might be valid too... – Chris M 11 hours ago
    
@sjb-2812 Please register your account by adding a login for it! Otherwise you could lose access to it. <shamelessad>Also, you might be interested in Retrocomputing.</shamelessad> – wizzwizz4 11 hours ago
1  
The Sinclair computers (starting with ZX80 and including also last 128 models) stored the BASIC tokens as single bytes. So NEW was typed by a single key press (A) and was stored as a single byte (0xE6) in the memory. Even ZX Spectrum 128 though it allowed typing the keywords by letters converted them internally to the single byte tokens. See for example ZX Spectrum character set on Wikipedia. – pabouk 7 hours ago

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

share|improve this answer
    
do you need the ;? – RaisingAgent 16 hours ago
1  
@LliwTelracs Pretty sure it is "shutdown /p" and not "-p" and the space is not needed. – P. Ktinos 16 hours ago
    
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 – LliwTelracs 16 hours ago
    
@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. – P. Ktinos 16 hours ago
1  
#include<cstdlib> will shave off a byte. – Snowman 12 hours ago

C# 79 bytes

This works on Windows:

class _{static void Main(){System.Diagnostics.Process.Start("shutdown","/s");}}
share|improve this answer

Node.js, 39 bytes

Linux

require('child_process').exec('init 0')
share|improve this answer

J, 14 bytes

2!:0'poweroff'

Works on Linux systems. 2!:0 executes the string in /bin/sh.

share|improve this answer

AppleScript, 45 bytes

tell application "Finder"
 shut down
end tell

I don't know how AppleScript syntax works or anything, so if anybody knows how to shorten this, please tell.

Also, I'm doing this on my phone at school ("hacking"), so I can't test it.

share|improve this answer

AHK, 19 15 bytes

Run,Shutdown -s

Works on windows only.

share|improve this answer

Mathematica on Windows 16 Bytes

Invoke the shell command with Run.

Run@"Shutdown/p"
share|improve this answer
    
Does this work on every OS, or a specific one? – Greg Martin 12 hours ago
    
@GregMartin Windows only. Maybe a Linux guy can comment on modifying this to pass the appropriate shell command there. – Kelly Lowder 12 hours ago
1  
I'm sure a Linux person of any gender could! Perhaps "Mathematica on Windows" is more precise in the post itself? – Greg Martin 10 hours ago
2  
Lol. I use "guy" in the gender-neutral sense. slate.com/blogs/lexicon_valley/2016/02/10/… – Kelly Lowder 8 hours ago
    
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 5 hours ago

Bash 15 bytes

shutdown -h now

alternatively : init 0

share|improve this answer
1  
useless use of sudo – Ipor Sircer 8 hours ago
    
you're right given that only one user is logged in! :) – Abel Tom 36 mins ago

R, 20 bytes

system("shutdown/s")
share|improve this answer

AutoHotkey, 11 Bytes

Shutdown, 9

AHK Supports a Shutdown command by default. The 9 is a flag, consisting of Shutdown=1 + PowerDown=8.

share|improve this answer

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.

share|improve this answer
1  
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 11 hours ago

JavaScript (non-competing), 9 7 bytes

Because of JavaScript's nature, closing the current tab is as close as you get (and as such, you can never really complete the challenge without abusing some bug in an OS). I still figured it's nice to have an answer in every language, so here goes:

close()
share|improve this answer
    
Wouldn't just close() work as well? I see no need to set a variable. – ETHproductions 12 hours ago
    
Oops, I intended to write =>, but apparently... I didn't. I thought it should be a function, so in that case, $ would be the (discarded) argument. It seems however, that I was wrong, and I should just remove the two bytes because you're right... Thanks. – L. Serné 12 hours ago
    
you can run javascript from outside the browser surely – Ewan 11 hours ago
    
    
Well, it can be a function, but in this case a program works just fine. Actually, you can just do close as a function entry, because it's technically a function you can call just like any other function entry. – ETHproductions 11 hours ago

AppleScript, 30 bytes

tell app "Finder" to shut down

Does exactly what it says: tells Finder to shut the computer off.

share|improve this answer

Batch, 8 bytes

shutdown

Hibernate ya' want?

shutdown -h

What about a restart?

shutdown -r
share|improve this answer
2  
Shutdown without a flag on Windows has the following description "No args" Display help. This is the same as typing /?". If you are on linux, you should specify the system type. For Windows, the shortest shutdown command that shuts down is shutdown/s or /p. – LliwTelracs 9 hours ago
    
Also, hibernate is invalid. -r, -p and -s flags are valid, because they shut down the computer. – P. Ktinos 9 hours ago
    
At least if it runs on Linux shutdown is a valid answer, as it defaults to -P which is power off. However, batch files are not made for Linux and need to be run as "wine cmd.exe /c Filename.bat" – LliwTelracs 9 hours ago
1  
@180Five Afaik Hibernate didn't shutdown the machine, it just put it in sleep mode. – P. Ktinos 8 hours ago
1  
@180Five it doesn't work on windows 10 gyazo.com/b44f6a9dd18030eb90a56028269397ca - if you can prove it works on some other version of windows, then this answer can be valid. – P. Ktinos 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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