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 code-golf, thus the lowest amount of bytes wins!
61 Answers 61
Assembly (x86/x64, Linux, as), (削除) 22 (削除ここまで) (削除) 21 (削除ここまで) 19 bytes
mov 0ドルx58, %al # 2 bytes: b0 58
mov 0ドルxfee1dead, %ebx # 5 bytes: bb ad de e1 fe
mov 0ドルx28121969, %ecx # 5 bytes: b9 69 19 12 28
mov 0ドルx4321fedc, %edx # 5 bytes: ba dc fe 21 43
int 0ドルx80 # 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 0ドルx80 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 0ドルx80.
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 0ドルx58, %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 0ドルx58, %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 0ドルxa9, %al # 2 bytes: b0 a9
mov 0ドルxfee1dead, %edi # 5 bytes: bf ad de e1 fe
mov 0ドルx28121969, %esi # 5 bytes: be 69 19 12 28
mov 0ドルx4321fedc, %edx # 5 bytes: ba dc fe 21 43
syscall # 2 bytes: 0f 05
The only differences lie in the instruction (syscall vs int 0ドルx80), the value of __NR_REBOOT (0xa9 vs 0x58), and the involved registers.
-
\$\begingroup\$ Do we normally count assembly as compiled bytes without the framing bytes required for exe header? \$\endgroup\$Joshua– Joshua2017年01月28日 14:42:34 +00:00Commented Jan 28, 2017 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– Dennis2017年01月28日 16:41:30 +00:00Commented Jan 28, 2017 at 16:41
-
\$\begingroup\$ Could you add the hex dump after assembly of the code? \$\endgroup\$ckjbgames– ckjbgames2017年02月13日 16:05:05 +00:00Commented Feb 13, 2017 at 16:05
-
\$\begingroup\$ @ckjbgames Here it is. \$\endgroup\$Dennis– Dennis2017年02月13日 19:21:34 +00:00Commented Feb 13, 2017 at 19:21
-
\$\begingroup\$ I
fee1sodeadright now. \$\endgroup\$arminb– arminb2018年06月27日 14:42:46 +00:00Commented Jun 27, 2018 at 14:42
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)
-
4\$\begingroup\$ Shorter one for Win7 & Win10 (and maybe more):
gcm *p-c*|iex-- and there's only the one result too! \$\endgroup\$Bob– Bob2017年01月25日 04:33:17 +00:00Commented Jan 25, 2017 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– briantist2017年01月25日 14:52:09 +00:00Commented Jan 25, 2017 at 14:52
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.
-
80\$\begingroup\$
Press the big button on the front of your computer= 0 bytes \$\endgroup\$Penguin9– Penguin92017年01月24日 13:53:17 +00:00Commented Jan 24, 2017 at 13:53 -
100\$\begingroup\$ @RaisingAgent Here at PPCG we solve our problems in code. Physical labour is frowned upon. \$\endgroup\$steenbergh– steenbergh2017年01月24日 13:54:36 +00:00Commented Jan 24, 2017 at 13:54
-
13\$\begingroup\$ @steenbergh I don't know... pressing one big button vs 10 keystrokes? I'll go with the button \$\endgroup\$WorldSEnder– WorldSEnder2017年01月24日 21:01:15 +00:00Commented Jan 24, 2017 at 21:01
-
19\$\begingroup\$ @RaisingAgent Your proposal has scalability issues. Try doing it for 10.000 computers four times a week. \$\endgroup\$Emilio M Bumachar– Emilio M Bumachar2017年01月26日 12:14:42 +00:00Commented Jan 26, 2017 at 12:14
-
10\$\begingroup\$ @RaisingAgent real men use the switch that's right on the PSU \$\endgroup\$Cruncher– Cruncher2017年01月26日 14:18:30 +00:00Commented Jan 26, 2017 at 14:18
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.
-
14\$\begingroup\$ Upvoted because of the limited platform. \$\endgroup\$pipe– pipe2017年01月26日 14:39:38 +00:00Commented Jan 26, 2017 at 14:39
-
\$\begingroup\$ Ha! Somebody beat Dennis! \$\endgroup\$Joshua– Joshua2017年01月30日 16:57:20 +00:00Commented Jan 30, 2017 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– Jakob2018年06月26日 19:03:08 +00:00Commented Jun 26, 2018 at 19:03
-
\$\begingroup\$ @Jakob You are right! \$\endgroup\$Margaret Bloom– Margaret Bloom2018年06月26日 21:26:09 +00:00Commented Jun 26, 2018 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– Prof. Falken2018年09月03日 12:40:23 +00:00Commented Sep 3, 2018 at 12:40
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 .
-
\$\begingroup\$ Noice. I was an Apple II hacker back in the day and didn't even remember this opcode. \$\endgroup\$kindall– kindall2017年01月25日 19:06:08 +00:00Commented Jan 25, 2017 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\$2xsaiko– 2xsaiko2017年01月25日 19:17:35 +00:00Commented Jan 25, 2017 at 19:17
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):
-
\$\begingroup\$ grub shell is not a "programming language" - it's not Turing complete \$\endgroup\$Alnitak– Alnitak2017年01月25日 11:38:34 +00:00Commented Jan 25, 2017 at 11:38
-
12\$\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 derivativesand supports variables, conditional constructs, functions and more, so obviously, it is Turning-complete, by any measure. \$\endgroup\$zeppelin– zeppelin2017年01月25日 12:17:22 +00:00Commented Jan 25, 2017 at 12:17 -
1\$\begingroup\$ OK, I'll take that back - I didn't find that stuff when I looked \$\endgroup\$Alnitak– Alnitak2017年01月25日 12:32:39 +00:00Commented Jan 25, 2017 at 12:32
-
31\$\begingroup\$ When it cannot display a spinner it is obviously not Turning-complete \$\endgroup\$JFBM– JFBM2017年01月26日 11:31:45 +00:00Commented Jan 26, 2017 at 11:31
-
1\$\begingroup\$ In Bash it's the same command. \$\endgroup\$Paweł Tokarz– Paweł Tokarz2017年01月30日 10:55:47 +00:00Commented Jan 30, 2017 at 10:55
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.
-
107\$\begingroup\$ "I'll add a short explanation later, my laptop is currently off." <- The danger of a shutdown challenge \$\endgroup\$Suever– Suever2017年01月24日 14:08:13 +00:00Commented Jan 24, 2017 at 14:08
-
4\$\begingroup\$ Axaxaxaxa, well, restarting your computer every once in a while can't be bad :) \$\endgroup\$Penguin9– Penguin92017年01月24日 14:14:25 +00:00Commented Jan 24, 2017 at 14:14
-
7\$\begingroup\$ Does ruin your
uptime, though \$\endgroup\$Wayne Werner– Wayne Werner2017年01月26日 19:02:23 +00:00Commented Jan 26, 2017 at 19:02 -
\$\begingroup\$ Why submit this as MATLAB, instead of submitting
shutdown -sas shell? \$\endgroup\$Stack Exchange Broke The Law– Stack Exchange Broke The Law2017年01月27日 05:50:16 +00:00Commented Jan 27, 2017 at 5:50 -
10\$\begingroup\$ Because for me, shell is either something I find on the beach, or a gas station. \$\endgroup\$Stewie Griffin– Stewie Griffin2017年01月27日 07:18:26 +00:00Commented Jan 27, 2017 at 7:18
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.
SmileBASIC, (削除) 14 (削除ここまで) (削除) 11 (削除ここまで) 6 bytes
OPTION
Triggers a crash causing the 3DS to restart. I hope this counts.
Works in the most recent version.
-
\$\begingroup\$ I thought this had been patched... Please include a version number. \$\endgroup\$wizzwizz4– wizzwizz42017年01月24日 19:05:21 +00:00Commented Jan 24, 2017 at 19:05
-
3\$\begingroup\$ It has been patched in version 3.5, which hasn't been released outside of Japan yet. \$\endgroup\$12Me21– 12Me212017年01月24日 19:35:54 +00:00Commented Jan 24, 2017 at 19:35
-
5\$\begingroup\$ @12Me21 Sure it counts. Nice choice of environment. \$\endgroup\$Offtkp– Offtkp2017年01月24日 21:46:15 +00:00Commented Jan 24, 2017 at 21:46
-
\$\begingroup\$ If only
REBOOTfrom SBv2 counted as a valid answer. \$\endgroup\$snail_– snail_2018年06月27日 04:09:48 +00:00Commented Jun 27, 2018 at 4:09
Bash, (削除) 7 (削除ここまで) 6 bytes
init 0
Assuming that the program is run as root.
-
\$\begingroup\$ Indeed, powering off the machine is required. Thus you need 'halt -p' as a valid answer \$\endgroup\$Offtkp– Offtkp2017年01月24日 14:07:00 +00:00Commented Jan 24, 2017 at 14:07
-
4\$\begingroup\$ This editing massacre :) \$\endgroup\$Penguin9– Penguin92017年01月24日 14:08:13 +00:00Commented Jan 24, 2017 at 14:08
-
\$\begingroup\$ @P.Ktinos:
poweroffis a single word, although slightly more letters. \$\endgroup\$ccpizza– ccpizza2017年01月27日 12:32:37 +00:00Commented Jan 27, 2017 at 12:32 -
1\$\begingroup\$ @ccpizza Right, but the typical scoring criteria is byte count \$\endgroup\$Suever– Suever2017年01月27日 14:45:01 +00:00Commented Jan 27, 2017 at 14:45
-
\$\begingroup\$ Also,
init 6would restart the machine, but not sure that would completely count as "turning off." \$\endgroup\$Chipster– Chipster2019年11月02日 05:42:51 +00:00Commented Nov 2, 2019 at 5:42
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
-
\$\begingroup\$ I think that using the Commodore 64's direct input mode from BASIC with
sys64738will 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– Shaun Bebbers2017年01月24日 17:03:57 +00:00Commented Jan 24, 2017 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– wizzwizz42017年01月24日 18:24:22 +00:00Commented Jan 24, 2017 at 18:24
-
3\$\begingroup\$ You don't need the space between the
0and thesys64738, you can replace thesyswith the shortcut formsY, 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– Mark2017年01月24日 22:46:15 +00:00Commented Jan 24, 2017 at 22:46 -
2\$\begingroup\$ Does not power off. \$\endgroup\$TecBrat– TecBrat2017年01月27日 15:03:41 +00:00Commented Jan 27, 2017 at 15:03
-
1\$\begingroup\$ I once spilled beer on my Commodore 64, that powered it off permanently. Does this count? \$\endgroup\$Shaun Bebbers– Shaun Bebbers2017年01月27日 15:06:52 +00:00Commented Jan 27, 2017 at 15:06
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')
-
2
-
1\$\begingroup\$ Ah ok, my bad.. \$\endgroup\$FlipTack– FlipTack2017年01月26日 19:59:38 +00:00Commented Jan 26, 2017 at 19:59
AppleScript, 30 bytes
tell app "Finder" to shut down
Does exactly what it says: tells Finder to shut the computer off.
-
\$\begingroup\$ This sounds scarily like someone instructing a MacOS user on how to shutdown the PC. \$\endgroup\$Joao-3– Joao-32025年10月19日 00:00:06 +00:00Commented Oct 19 at 0:00
-
1\$\begingroup\$ @Joao-3, it should: AppleScript was a foray into natural-language programming. (And like with every other such effort, its advocates quickly discovered that the hard part of programming wasn't the funny symbols that traditional languages use, it was learning to think of tasks in a structured way.) \$\endgroup\$Mark– Mark2025年10月20日 22:44:35 +00:00Commented Oct 20 at 22:44
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.
-
2\$\begingroup\$ It's shorter to replace
classwithinterfaceas that allows you to omitpublicfrom main. \$\endgroup\$Pavel– Pavel2017年01月24日 15:42:39 +00:00Commented Jan 24, 2017 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– user189322017年01月24日 18:12:31 +00:00Commented Jan 24, 2017 at 18:12 -
1\$\begingroup\$ Wouldn't the Linux version be a bit shorter? \$\endgroup\$jpmc26– jpmc262017年01月25日 01:32:59 +00:00Commented Jan 25, 2017 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– Olivier Grégoire2017年01月26日 12:23:27 +00:00Commented Jan 26, 2017 at 12:23 -
1\$\begingroup\$ This should be labeled as a Linux-specific answer, e.g. "Java on LInux". \$\endgroup\$Jakob– Jakob2018年06月26日 19:05:58 +00:00Commented Jun 26, 2018 at 19:05
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
-
\$\begingroup\$ Isn't that 7 bytes? \$\endgroup\$Linnea Gräf– Linnea Gräf2017年01月26日 14:52:55 +00:00Commented Jan 26, 2017 at 14:52
-
\$\begingroup\$ meta.codegolf.stackexchange.com/a/4764/62824 @RomanGräf \$\endgroup\$devRicher– devRicher2017年01月26日 14:55:41 +00:00Commented Jan 26, 2017 at 14:55
-
\$\begingroup\$ Sounds convincing to me. \$\endgroup\$Linnea Gräf– Linnea Gräf2017年01月26日 14:58:28 +00:00Commented Jan 26, 2017 at 14:58
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 ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$
-
1\$\begingroup\$ Now i am curious about IncludeOS \$\endgroup\$Karl Napf– Karl Napf2017年01月25日 19:57:36 +00:00Commented Jan 25, 2017 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– simon2017年01月25日 20:05:12 +00:00Commented Jan 25, 2017 at 20:05
-
1\$\begingroup\$ You don't need to use
void Service::start,int mainworks in IncludeOS as well. \$\endgroup\$Ingve– Ingve2017年01月27日 10:56:11 +00:00Commented Jan 27, 2017 at 10:56
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.
-
1\$\begingroup\$
AHKis mysterious. \$\endgroup\$Gurupad Mamadapur– Gurupad Mamadapur2017年01月25日 14:35:46 +00:00Commented Jan 25, 2017 at 14:35 -
\$\begingroup\$ Do you need the space after the comma? \$\endgroup\$2xsaiko– 2xsaiko2017年01月26日 15:34:13 +00:00Commented Jan 26, 2017 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– Gurupad Mamadapur2017年01月26日 15:52:24 +00:00Commented Jan 26, 2017 at 15:52
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).
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");
-
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– TheLethalCoder2017年01月25日 14:18:45 +00:00Commented Jan 25, 2017 at 14:18 -
\$\begingroup\$ You can also remove the unnecessary space. \$\endgroup\$nothrow– nothrow2017年01月27日 20:59:18 +00:00Commented Jan 27, 2017 at 20:59
-
\$\begingroup\$ Of course yes! Thank you, @nothrow :) \$\endgroup\$Dariusz Woźniak– Dariusz Woźniak2017年01月27日 22:38:32 +00:00Commented Jan 27, 2017 at 22:38
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).
-
1\$\begingroup\$
0(logoff) is a special case here; it does not combine with Standby or Hibernate implicitly. \$\endgroup\$wizzwizz4– wizzwizz42017年01月24日 19:02:38 +00:00Commented Jan 24, 2017 at 19:02 -
\$\begingroup\$ OK
0 + 32 = 32\$\endgroup\$rahnema1– rahnema12017年01月24日 19:25:23 +00:00Commented Jan 24, 2017 at 19:25 -
\$\begingroup\$ Hm, so does it always log off before going to standby/hibernation? \$\endgroup\$Ruslan– Ruslan2017年01月29日 06:30:57 +00:00Commented Jan 29, 2017 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– rahnema12017年01月29日 07:05:56 +00:00Commented Jan 29, 2017 at 7:05
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:\WinDDK7600円.16385.1\lib\win7\i386\nt.lib C:\WinDDK7600円.16385.1\lib\win7\i386\ntdllp.lib C:\WinDDK7600円.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!
J, 14 bytes
2!:0'poweroff'
Works on Linux systems. 2!:0 executes the string in /bin/sh.
Groovy, 23 bytes
'shutdown /p'.execute()
-
2\$\begingroup\$ Is the space before the slash necessary? \$\endgroup\$steenbergh– steenbergh2017年01月24日 14:12:20 +00:00Commented Jan 24, 2017 at 14:12
-
3\$\begingroup\$ @steenbergh Yes, it is needed. \$\endgroup\$Gurupad Mamadapur– Gurupad Mamadapur2017年01月24日 14:32:57 +00:00Commented Jan 24, 2017 at 14:32
-
\$\begingroup\$ @GurupadMamadapur are you sure? \$\endgroup\$TrojanByAccident– TrojanByAccident2017年01月24日 19:48:13 +00:00Commented Jan 24, 2017 at 19:48
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.
-
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– Joshua2017年01月25日 18:57:08 +00:00Commented Jan 25, 2017 at 18:57
-
5\$\begingroup\$ I believe you have to add "/proc/" to your byte count \$\endgroup\$zeppelin– zeppelin2017年01月25日 20:49:17 +00:00Commented Jan 25, 2017 at 20:49
-
\$\begingroup\$ @Joshua It might be shell dependent. I actually tested it (with
bash) on my computer :-) \$\endgroup\$Radovan Garabík– Radovan Garabík2017年01月25日 21:12:12 +00:00Commented Jan 25, 2017 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– zeppelin2017年01月26日 14:52:12 +00:00Commented Jan 26, 2017 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– zeppelin2017年01月26日 15:40:21 +00:00Commented Jan 26, 2017 at 15:40
Android shell, 9 bytes
reboot -p
-
7\$\begingroup\$ Welcome to PPCG! \$\endgroup\$ETHproductions– ETHproductions2017年01月29日 20:46:51 +00:00Commented Jan 29, 2017 at 20:46
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
-
\$\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– devRicher2017年01月25日 11:08:54 +00:00Commented Jan 25, 2017 at 11:08
-
\$\begingroup\$ You can probably remove the indentation \$\endgroup\$Cyoce– Cyoce2017年01月25日 15:52:18 +00:00Commented Jan 25, 2017 at 15:52
-
4\$\begingroup\$
tell app "Finder" to shut downshould work, I believe (though the compilation may replace 'app' with 'application'). \$\endgroup\$kindall– kindall2017年01月25日 19:04:11 +00:00Commented Jan 25, 2017 at 19:04
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"))
Node.js, 39 bytes
Linux
require('child_process').exec('init 0')
AHK, (削除) 19 (削除ここまで) 15 bytes
Run,Shutdown -s
Works on windows only.
`whatever`;Bash/Perl/PHP/Ruby/etc. stupiglots. \$\endgroup\$shatdown: past tense of the verb shutdown\$\endgroup\$