85
\$\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

92
\$\begingroup\$

Assembly (x86/x64, Linux, as), 22 21 19 bytes

mov  $0x58,       %al  # 2 bytes: b0 58
mov  $0xfee1dead, %ebx # 5 bytes: bb ad de e1 fe
mov  $0x28121969, %ecx # 5 bytes: b9 69 19 12 28
mov  $0x4321fedc, %edx # 5 bytes: ba dc fe 21 43
int  $0x80             # 2 bytes: cd 80

Must be run as root.

This is equivalent to pressing the power button and not a safe way to power off your PC. Make sure you close all open applications and execute sync to flush all file system buffers before executing this program, to at least minimize the risk of file corruption.

Test run

$ as -o poweroff.o poweroff.s
$ ld -o poweroff   poweroff.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078
$ sudo sh -c 'sync && ./poweroff'
root's password:

Followed by darkness.

How it works

int $0x80 invokes a software interrupt. It works on both x86 and x64, but has been deprecated for over a decade now and should not be used in production code. x64 code should use syscall instead. x86 should use sysenter, but it is too cumbersome for code golf.

The resulting action from the syscall depends on registers EAX - EDX, ESI, and EDI. The Linux Syscall Reference shows all syscalls that are available via int $0x80.

When EAX holds 0x58 (88), reboot is called, which can also be used to power off, put to sleep, or hibernate the computer, as well as switching kernels and disabling or enabling the Ctrl - Alt - Del key combo.

At the start of the program – and by compiling with as or gcc -nostdlib, we can make sure that we're actually at the start of the program – most registers are set to 0. This includes EAX, so we can use mov $0x58, %al to set the lower 8 bits of EAX to 0x58, thus setting EAX itself to 0x58. This saves two bytes over manually zeroing the register with xor %eax, %eax and one more over the straighforward mov $0x58, %eax which encodes 0x58 in 32 bits.

The first two arguments to reboot are magic numbers, presumably to prevent accidental reboots, and are read from registers EBX and ECX. Unless these numbers are equal to certain constants, reboot refuses to perform any action.

  • The first magic number must equal 0xfee1dead (feel dead), probably referring to the power off / death of the PC.

  • The second magic number can be equal to four different constants, although the latter three did not work in ancient versions of Linux. All of them seem to refer to the subsequent power on / birth of the PC.

    • 0x28121969 represents Linus Torvalds's birthday (December 28, 1969).

    • 0x05121996 represents Patricia Torvalds's birthday (December 5, 1996).

    • 0x16041998 represents Daniela Torvalds's birthday (April 16, 1998).

    • 0x20112000 represents Celeste Torvalds's birthday (November 20, 2000).

    Patricia, Daniela, and Celeste Torvalds are Linus Torvalds's three daughters.

  • The EDX register selects the type of "reboot" we want. 0x4321fedc is RB_POWER_OFF, shutting the PC down and powering it off.

  • Finally, the value of the ESI register is ignored for RB_POWER_OFF; the value of the EDI register is ignored entirely by reboot.

Alternate version, x64-only, 19 bytes

On x64, we can use a proper syscall for the same byte count.

mov     $0xa9,       %al  # 2 bytes: b0 a9
mov     $0xfee1dead, %edi # 5 bytes: bf ad de e1 fe
mov     $0x28121969, %esi # 5 bytes: be 69 19 12 28
mov     $0x4321fedc, %edx # 5 bytes: ba dc fe 21 43
syscall                   # 2 bytes: 0f 05

The only differences lie in the instruction (syscall vs int $0x80), the value of __NR_REBOOT (0xa9 vs 0x58), and the involved registers.

\$\endgroup\$
  • \$\begingroup\$ Do we normally count assembly as compiled bytes without the framing bytes required for exe header? \$\endgroup\$ – Joshua Jan 28 '17 at 14:42
  • \$\begingroup\$ Yes, because each instruction corresponds to specific bytes in the binary file. ELF files (or whatever format your OS uses) are considered a different language and would have to be created manually to achieve a decent score. \$\endgroup\$ – Dennis Jan 28 '17 at 16:41
  • \$\begingroup\$ Could you add the hex dump after assembly of the code? \$\endgroup\$ – ckjbgames Feb 13 '17 at 16:05
  • \$\begingroup\$ @ckjbgames Here it is. \$\endgroup\$ – Dennis Feb 13 '17 at 19:21
  • \$\begingroup\$ I fee1 so dead right now. \$\endgroup\$ – arminb Jun 27 '18 at 14:42
50
\$\begingroup\$

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!

The trick of course, is getting a list of commands that don't have any mandatory parameters, otherwise PowerShell will prompt for a value.

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)

\$\endgroup\$
  • 4
    \$\begingroup\$ Shorter one for Win7 & Win10 (and maybe more): gcm *p-c*|iex -- and there's only the one result too! \$\endgroup\$ – Bob Jan 25 '17 at 4:33
  • \$\begingroup\$ @Bob highly dependent on installed modules still; on my Win10 machine it returns 6 results, several of which will have mandatory parameters (so they'll prompt and never make it to Stop-Computer, which is the last result). \$\endgroup\$ – briantist Jan 25 '17 at 14:52
39
\$\begingroup\$

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.

\$\endgroup\$
  • 79
    \$\begingroup\$ Press the big button on the front of your computer = 0 bytes \$\endgroup\$ – RaisingAgent Jan 24 '17 at 13:53
  • 96
    \$\begingroup\$ @RaisingAgent Here at PPCG we solve our problems in code. Physical labour is frowned upon. \$\endgroup\$ – steenbergh Jan 24 '17 at 13:54
  • 12
    \$\begingroup\$ @steenbergh I don't know... pressing one big button vs 10 keystrokes? I'll go with the button \$\endgroup\$ – WorldSEnder Jan 24 '17 at 21:01
  • 16
    \$\begingroup\$ @RaisingAgent Your proposal has scalability issues. Try doing it for 10.000 computers four times a week. \$\endgroup\$ – Emilio M Bumachar Jan 26 '17 at 12:14
  • 10
    \$\begingroup\$ @RaisingAgent real men use the switch that's right on the PSU \$\endgroup\$ – Cruncher Jan 26 '17 at 14:18
35
\$\begingroup\$

x86 machine code, Lenovo Z510, DOS COM or Bootloader, 7 bytes

BITS 16

mov ax, (1 << 13) | (7 << 10)
mov dx, 1804h
out dx, ax

Assembled into B8 00 3C BA 04 18 EF

I'm exploiting "You are allowed to write code that only works in a specific environment/OS, if you wish" to the ultimate level: this will only work on Lenovo Z5101.

This writes SLP_TYPa << 10 | SLP_EN to the PM1a_CNT ACPI register, I dumped this values from the ACPI tables months ago for an answer about shutdown (where I give a bit of context to the code above).

This can be run either under DOS as a COM (just write the binary data into a file and change extension to "com") or as a bootloader.
For the latter write the standard signature and beware of the issues with modern firmware.


1 Not strictly true, any system that happens to have the same ACPI configuration will also shutdown.

\$\endgroup\$
  • 13
    \$\begingroup\$ Upvoted because of the limited platform. \$\endgroup\$ – pipe Jan 26 '17 at 14:39
  • \$\begingroup\$ Ha! Somebody beat Dennis! \$\endgroup\$ – Joshua Jan 30 '17 at 16:57
  • \$\begingroup\$ If you're going with the 7-byte count, you should call this a machine code submission rather than an assembly solution. The assembly submission is 63 bytes. \$\endgroup\$ – Jakob Jun 26 '18 at 19:03
  • \$\begingroup\$ @Jakob You are right! \$\endgroup\$ – Margaret Bloom Jun 26 '18 at 21:26
  • \$\begingroup\$ You look like the right person to answer this question: stackoverflow.com/questions/2019463/… (I failed miserably at it, had to delete my "answer" out of shame.) \$\endgroup\$ – Prof. Falken Sep 3 '18 at 12:40
34
\$\begingroup\$

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.

Bonus

Here is a proof that GRUB shell is indeed Turning-complete (see comments):

enter image description here

\$\endgroup\$
  • \$\begingroup\$ grub shell is not a "programming language" - it's not Turing complete \$\endgroup\$ – Alnitak Jan 25 '17 at 11:38
  • 10
    \$\begingroup\$ @Alnitak, what makes you think so ? It features a rather advanced built-in scripting language, which has a syntax quite similar to that of GNU Bash and other Bourne shell derivatives and supports variables, conditional constructs, functions and more, so obviously, it is Turning-complete, by any measure. \$\endgroup\$ – zeppelin Jan 25 '17 at 12:17
  • 1
    \$\begingroup\$ OK, I'll take that back - I didn't find that stuff when I looked \$\endgroup\$ – Alnitak Jan 25 '17 at 12:32
  • 29
    \$\begingroup\$ When it cannot display a spinner it is obviously not Turning-complete \$\endgroup\$ – J_F_B_M Jan 26 '17 at 11:31
  • 1
    \$\begingroup\$ In Bash it's the same command. \$\endgroup\$ – Paweł Tokarz Jan 30 '17 at 10:55
32
\$\begingroup\$

65C02 machine code, 1 byte

STP instruction (0xDB):

STP stops the clock input of the 65C02, effectively shutting down the 65C02 until a hardware reset occurs (i.e. the RES pin goes low). This puts the 65C02 into a low power state. This is useful for applications (circuits) that require low power consumption, but STP is rarely seen otherwise.

(Source: http://www.6502.org/tutorials/65c02opcodes.html)

xxd dump:

00000000: db                                       .
\$\endgroup\$
  • \$\begingroup\$ Noice. I was an Apple II hacker back in the day and didn't even remember this opcode. \$\endgroup\$ – kindall Jan 25 '17 at 19:06
  • 2
    \$\begingroup\$ Yeah, after writing a 65c816 emulator with more features and saw this challenge I knew what to do :P Also, looks like this is the only 1 byte answer so far! \o/ \$\endgroup\$ – therealfarfetchd Jan 25 '17 at 19:17
25
\$\begingroup\$

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.

\$\endgroup\$
  • 105
    \$\begingroup\$ "I'll add a short explanation later, my laptop is currently off." <- The danger of a shutdown challenge \$\endgroup\$ – Suever Jan 24 '17 at 14:08
  • 4
    \$\begingroup\$ Axaxaxaxa, well, restarting your computer every once in a while can't be bad :) \$\endgroup\$ – RaisingAgent Jan 24 '17 at 14:14
  • 6
    \$\begingroup\$ Does ruin your uptime, though \$\endgroup\$ – Wayne Werner Jan 26 '17 at 19:02
  • \$\begingroup\$ Why submit this as MATLAB, instead of submitting shutdown -s as shell? \$\endgroup\$ – user253751 Jan 27 '17 at 5:50
  • 10
    \$\begingroup\$ Because for me, shell is either something I find on the beach, or a gas station. \$\endgroup\$ – Stewie Griffin Jan 27 '17 at 7:18
21
\$\begingroup\$

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.
\$\endgroup\$
19
\$\begingroup\$

SmileBASIC, 14 11 6 bytes

OPTION

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

Works in the most recent version.

\$\endgroup\$
  • \$\begingroup\$ I thought this had been patched... Please include a version number. \$\endgroup\$ – wizzwizz4 Jan 24 '17 at 19:05
  • 3
    \$\begingroup\$ It has been patched in version 3.5, which hasn't been released outside of Japan yet. \$\endgroup\$ – 12Me21 Jan 24 '17 at 19:35
  • 4
    \$\begingroup\$ @12Me21 Sure it counts. Nice choice of environment. \$\endgroup\$ – P. Ktinos Jan 24 '17 at 21:46
  • \$\begingroup\$ If only REBOOT from SBv2 counted as a valid answer. \$\endgroup\$ – snail_ Jun 27 '18 at 4:09
16
\$\begingroup\$

Bash, 7 6 bytes

init 0

Assuming that the program is run as root.

\$\endgroup\$
  • \$\begingroup\$ Indeed, powering off the machine is required. Thus you need 'halt -p' as a valid answer \$\endgroup\$ – P. Ktinos Jan 24 '17 at 14:07
  • 4
    \$\begingroup\$ This editing massacre :) \$\endgroup\$ – RaisingAgent Jan 24 '17 at 14:08
  • \$\begingroup\$ @P.Ktinos: poweroff is a single word, although slightly more letters. \$\endgroup\$ – ccpizza Jan 27 '17 at 12:32
  • 1
    \$\begingroup\$ @ccpizza Right, but the typical scoring criteria is byte count \$\endgroup\$ – Suever Jan 27 '17 at 14:45
  • \$\begingroup\$ Also, init 6 would restart the machine, but not sure that would completely count as "turning off." \$\endgroup\$ – Chipster Nov 2 at 5:42
13
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Shaun Bebbers Jan 24 '17 at 17:03
  • 2
    \$\begingroup\$ 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 ``. \$\endgroup\$ – wizzwizz4 Jan 24 '17 at 18:24
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Mark Jan 24 '17 at 22:46
  • 2
    \$\begingroup\$ Does not power off. \$\endgroup\$ – TecBrat Jan 27 '17 at 15:03
  • 1
    \$\begingroup\$ I once spilled beer on my Commodore 64, that powered it off permanently. Does this count? \$\endgroup\$ – Shaun Bebbers Jan 27 '17 at 15:06
13
\$\begingroup\$

Java, 101 98 62 42 bytes

()->{Runtime.getRuntime().exec("init 0");}

As @Snowman pointed out, the question didn't specify if a full program was needed, so this should still be acceptable.

\$\endgroup\$
  • 2
    \$\begingroup\$ It's shorter to replace class with interface as that allows you to omit public from main. \$\endgroup\$ – Pavel Jan 24 '17 at 15:42
  • 6
    \$\begingroup\$ 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");} \$\endgroup\$ – user18932 Jan 24 '17 at 18:12
  • 1
    \$\begingroup\$ Wouldn't the Linux version be a bit shorter? \$\endgroup\$ – jpmc26 Jan 25 '17 at 1:32
  • 2
    \$\begingroup\$ ()->{Runtime.getRuntime().exec("init 0");} or tentatively ()->Runtime.getRuntime().exec("init 0") (can't check at the moment). It's a lambda implementing a custom void, no-args-throwing interface. \$\endgroup\$ – Olivier Grégoire Jan 26 '17 at 12:23
  • 1
    \$\begingroup\$ This should be labeled as a Linux-specific answer, e.g. "Java on LInux". \$\endgroup\$ – Jakob Jun 26 '18 at 19:05
13
\$\begingroup\$

Python 2, 29 bytes

import os;os.system('init 0')

Works on Linux systems and needs root privileges.

For Windows, 33 bytes

import os;os.system('shutdown/p')
\$\endgroup\$
  • 2
    \$\begingroup\$ Someone else posted the Linux shell version. \$\endgroup\$ – jpmc26 Jan 25 '17 at 1:34
  • 1
    \$\begingroup\$ Ah ok, my bad.. \$\endgroup\$ – FlipTack Jan 26 '17 at 19:59
12
\$\begingroup\$

TI-Nspire CX CAS, 3 bytes

" "+" "

summing two empty strings.

On an older version of the calculator, this would crash the device and cause it to reboot (there's no way to shutdown a device without reboot apart from physical button combinations).

Video by Adriweb

\$\endgroup\$
12
\$\begingroup\$

C++, 51 40 bytes

-11 bytes thanks to Ingve!

#include<os>
int main(){OS::shutdown();}

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

Test:

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

$ cat service.cpp
#include<os>
int main(){OS::shutdown();}

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

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

$
\$\endgroup\$
  • 1
    \$\begingroup\$ Now i am curious about IncludeOS \$\endgroup\$ – Karl Napf Jan 25 '17 at 19:57
  • \$\begingroup\$ @KarlNapf Alfred held a great presentation at CppCon 2016 about IncludeOS: youtube.com/watch?v=t4etEwG2_LY if you want to know more :-) \$\endgroup\$ – simon Jan 25 '17 at 20:05
  • 1
    \$\begingroup\$ You don't need to use void Service::start, int main works in IncludeOS as well. \$\endgroup\$ – Ingve Jan 27 '17 at 10:56
10
\$\begingroup\$

AppleScript, 30 bytes

tell app "Finder" to shut down

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

\$\endgroup\$
9
\$\begingroup\$

AutoHotkey, 11 10 Bytes

Saved a byte thanks to Gurupad Mamadapur

Shutdown,9

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

\$\endgroup\$
  • 1
    \$\begingroup\$ AHK is mysterious. \$\endgroup\$ – Gurupad Mamadapur Jan 25 '17 at 14:35
  • \$\begingroup\$ Do you need the space after the comma? \$\endgroup\$ – therealfarfetchd Jan 26 '17 at 15:34
  • \$\begingroup\$ space after , is not needed. Try testing it with a different flag. I thought this will not immediately shut down the computer, and put it in my main script which loads at startup :D Now I've to load in safe mode and solve the problem. Sigh. \$\endgroup\$ – Gurupad Mamadapur Jan 26 '17 at 15:52
9
\$\begingroup\$

Machine code (x86, boot loader) 18 bytes

b8 01 53 33 db cd 15 b8 07 53 bb 01 00 b9 03 00 cd 15

Disassembly:

mov ax, 0x5301 ; connect to real-mode APM services
xor bx, bx     ; device id 0 - APM BIOS
int 0x15       ; call APM
mov ax, 0x5307 ; set power state
mov bx, 0x0001 ; on all devices
mov cx, 0x0003 ; to Off
int 0x15       ; call APM

You can test it in Bochs' debugger with the following commands (after starting simulation, having some disk drive set up):

pb 0x7c00
c
setpmem 0x7c00 4 0x335301b8
setpmem 0x7c04 4 0xb815cddb
setpmem 0x7c08 4 0x01bb5307
setpmem 0x7c0c 4 0x0003b900
setpmem 0x7c10 2 0x15cd
c

This same machine code will also work as a DOS executable (*.COM).

\$\endgroup\$
7
\$\begingroup\$

C#, 79 54 53 bytes

This works on Windows.

79 bytes:

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

53 bytes, thanks to TheLethalCoder:

_=>System.Diagnostics.Process.Start("shutdown","/s");
\$\endgroup\$
  • 2
    \$\begingroup\$ Note the challenge doesn't require a full program and doesn't disallow taking an input (unused of course) so you could compile to an Action<int> and do _=>System.Diagnostics.Process.Start("shutdown", "/s"); for 54 bytes. \$\endgroup\$ – TheLethalCoder Jan 25 '17 at 14:18
  • \$\begingroup\$ @TheLethalCoder, updated, thanks! \$\endgroup\$ – Dariusz Woźniak Jan 25 '17 at 15:57
  • \$\begingroup\$ You can also remove the unnecessary space. \$\endgroup\$ – nothrow Jan 27 '17 at 20:59
  • \$\begingroup\$ Of course yes! Thank you, @nothrow :) \$\endgroup\$ – Dariusz Woźniak Jan 27 '17 at 22:38
6
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ 0 (logoff) is a special case here; it does not combine with Standby or Hibernate implicitly. \$\endgroup\$ – wizzwizz4 Jan 24 '17 at 19:02
  • \$\begingroup\$ OK 0 + 32 = 32 \$\endgroup\$ – rahnema1 Jan 24 '17 at 19:25
  • \$\begingroup\$ Hm, so does it always log off before going to standby/hibernation? \$\endgroup\$ – Ruslan Jan 29 '17 at 6:30
  • \$\begingroup\$ @Ruslan I do not know exactly how it works, You can refer to AutoIt user forum to receive more feedbacks:-) \$\endgroup\$ – rahnema1 Jan 29 '17 at 7:05
6
\$\begingroup\$

Windows - NativeAPI, 212 bytes

Ok this is not really the shortest version. But maybe interesting to some. This will not execute on the Win32 subsystem. However it will run when executed within the context of the native api (like autochk.exe does)

Code

#define WIN32_NO_STATUS
#include <windef.h>
#include <winnt.h>
#include <ntndk.h>
NTSTATUS main() {
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE,TRUE,FALSE,NULL);
return ZwShutdownSystem(ShutdownPowerOff);
}

Build

To build this either use the WinDDK build.exe tool (with an appropriate source file for NMAKE)

Or use these commands to compile and link:

cl /Gd /D_X86_ /showIncludes /I%DDK_INC_PATH% /I%CRT_INC_PATH% /c main.c
link /verbose /nodefaultlib /subsystem:native /machine:X86 /entry:NtProcessStartup@4 C:\WinDDK\7600.16385.1\lib\win7\i386\nt.lib C:\WinDDK\7600.16385.1\lib\win7\i386\ntdllp.lib C:\WinDDK\7600.16385.1\lib\win7\i386\BufferOverflow.lib .\main.obj 

Note: This will build an x86 native executable. Change the appropriate bits if you want to build this for a different architecture.

Test

To test a native executable you have to execute it before the win32 subsystem is loaded. One common way to do it is to append the executable in the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute

Warning

If you do this however, you need to have an option to edit the registry hive of the target OS from another OS. Since you will not be able to boot the target OS anymore!

I did it on a VM where i could easily revert to a previous snapshot!

\$\endgroup\$
5
\$\begingroup\$

Groovy, 23 bytes

'shutdown /p'.execute()
\$\endgroup\$
  • 2
    \$\begingroup\$ Is the space before the slash necessary? \$\endgroup\$ – steenbergh Jan 24 '17 at 14:12
  • 3
    \$\begingroup\$ @steenbergh Yes, it is needed. \$\endgroup\$ – Gurupad Mamadapur Jan 24 '17 at 14:32
  • \$\begingroup\$ @GurupadMamadapur are you sure? \$\endgroup\$ – TrojanByAccident Jan 24 '17 at 19:48
5
\$\begingroup\$

Most Linux shells, 15 bytes

# echo o>/p*/*ger

It could be made to be 11 bytes long, if you happen to be in the /proc directory:

# echo o>*ger

This is very straightforward (has no side effects, like unmounting filesystems or killing processes) and rather brutal.

\$\endgroup\$
  • 1
    \$\begingroup\$ Last time I tried wildcard expansion on redirect it generated a file with the wildcard in it despite a matching file existing. \$\endgroup\$ – Joshua Jan 25 '17 at 18:57
  • 5
    \$\begingroup\$ I believe you have to add "/proc/" to your byte count \$\endgroup\$ – zeppelin Jan 25 '17 at 20:49
  • \$\begingroup\$ @Joshua It might be shell dependent. I actually tested it (with bash) on my computer :-) \$\endgroup\$ – Radovan Garabík Jan 25 '17 at 21:12
  • 1
    \$\begingroup\$ @Radovan Garabík - I don't think that is really valid. This is basically the same as implying that there is an alias "p=poweroff" (which is not "impossible" too) and claiming "p" to be a valid 1 byte solution. \$\endgroup\$ – zeppelin Jan 26 '17 at 14:52
  • 1
    \$\begingroup\$ But you can probably make it valid at expense of just 3 bytes, like this echo o>/p*/*ger \$\endgroup\$ – zeppelin Jan 26 '17 at 15:40
5
\$\begingroup\$

Android shell, 9 bytes

reboot -p
\$\endgroup\$
  • 7
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – ETHproductions Jan 29 '17 at 20:46
5
\$\begingroup\$

AppleScript, 44 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.

At 14 fewer bytes (thanks to kindall) you can say

tell app "Finder" to shut down
\$\endgroup\$
  • \$\begingroup\$ Someone came up with a shorter solution (with same program) a few hours after you (but posted it instead of commenting here) codegolf.stackexchange.com/a/107958/62824 \$\endgroup\$ – devRicher Jan 25 '17 at 11:08
  • \$\begingroup\$ You can probably remove the indentation \$\endgroup\$ – Cyoce Jan 25 '17 at 15:52
  • 4
    \$\begingroup\$ tell app "Finder" to shut down should work, I believe (though the compilation may replace 'app' with 'application'). \$\endgroup\$ – kindall Jan 25 '17 at 19:04
4
\$\begingroup\$

J, 14 bytes

2!:0'poweroff'

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

\$\endgroup\$
4
\$\begingroup\$

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"))
\$\endgroup\$
2
\$\begingroup\$

Node.js, 39 bytes

Linux

require('child_process').exec('init 0')
\$\endgroup\$
2
\$\begingroup\$

AHK, 19 15 bytes

Run,Shutdown -s

Works on windows only.

\$\endgroup\$
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

\$\endgroup\$
  • \$\begingroup\$ do you need the ;? \$\endgroup\$ – RaisingAgent Jan 24 '17 at 14:12
  • 1
    \$\begingroup\$ @LliwTelracs Pretty sure it is "shutdown /p" and not "-p" and the space is not needed. \$\endgroup\$ – P. Ktinos Jan 24 '17 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\$ – fəˈnɛtɪk Jan 24 '17 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\$ – P. Ktinos Jan 24 '17 at 14:25
  • 1
    \$\begingroup\$ #include<cstdlib> will shave off a byte. \$\endgroup\$ – user18932 Jan 24 '17 at 18:14

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.