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
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
-
\$\begingroup\$ do you need the
;? \$\endgroup\$Penguin9– Penguin92017年01月24日 14:12:58 +00:00Commented Jan 24, 2017 at 14:12 -
1\$\begingroup\$ @LliwTelracs Pretty sure it is "shutdown /p" and not "-p" and the space is not needed. \$\endgroup\$Offtkp– Offtkp2017年01月24日 14:14:36 +00:00Commented Jan 24, 2017 at 14:14
-
\$\begingroup\$ The semicolon is required for proper c++ syntax, using a slash allows for a shorter answer for windows but makes it no longer work for linux. Although I added the wrong character for shutdown that is shared between windows and linux \$\endgroup\$fəˈnɛtɪk– fəˈnɛtɪk2017年01月24日 14:21:54 +00:00Commented Jan 24, 2017 at 14:21
-
\$\begingroup\$ @LliwTelracs Although you can do whatever you want, I suggest abusing the 2nd rule and not caring about Linux functionality, since this is a code-golf and low amount of bytes is better. \$\endgroup\$Offtkp– Offtkp2017年01月24日 14:25:54 +00:00Commented Jan 24, 2017 at 14:25
-
1\$\begingroup\$
#include<cstdlib>will shave off a byte. \$\endgroup\$user18932– user189322017年01月24日 18:14:00 +00:00Commented Jan 24, 2017 at 18:14
Mathematica on Windows 16 Bytes
Invoke the shell command with Run.
Run@"Shutdown/p"
-
\$\begingroup\$ Does this work on every OS, or a specific one? \$\endgroup\$Greg Martin– Greg Martin2017年01月24日 18:18:03 +00:00Commented Jan 24, 2017 at 18:18
-
1\$\begingroup\$ I'm sure a Linux person of any gender could! Perhaps "Mathematica on Windows" is more precise in the post itself? \$\endgroup\$Greg Martin– Greg Martin2017年01月24日 20:20:58 +00:00Commented Jan 24, 2017 at 20:20
-
3\$\begingroup\$ Lol. I use "guy" in the gender-neutral sense. slate.com/blogs/lexicon_valley/2016/02/10/… \$\endgroup\$Kelly Lowder– Kelly Lowder2017年01月24日 22:56:01 +00:00Commented Jan 24, 2017 at 22:56
-
1\$\begingroup\$ Although this term is still being debated, it is certainly the case that "guys" is not uniformly gender-neutral; and research definitely shows that gendered language has a harmful effect on STEM disciplines and the women participating in them. I believe it's important to bend over backwards to be inclusive with our language, since the gender equity problem is not solving itself. \$\endgroup\$Greg Martin– Greg Martin2017年01月25日 01:04:11 +00:00Commented Jan 25, 2017 at 1:04
-
1\$\begingroup\$ Linux shutdown code:
shutdown +0is the traditional way to do a shutdown and power off (using the tersest representations of the flags);shutdownwith no arguments will delay 60 seconds then shut down. On recent systemd-based systems you're supposed to usesystemctl haltorsystemctl poweroffinstead, but that's more bytes so who cares about that. Note that any of these commands require root permissions (as shutting down the system could cause data loss for anyone else logged in at the time, it's an action that affects other users, so you need enough permissions to be able to do that). \$\endgroup\$user62131– user621312017年01月25日 06:08:49 +00:00Commented Jan 25, 2017 at 6:08
Bash 15 bytes
shutdown -h now
alternatively : init 0
-
1\$\begingroup\$ useless use of
sudo\$\endgroup\$Ipor Sircer– Ipor Sircer2017年01月24日 22:03:33 +00:00Commented Jan 24, 2017 at 22:03 -
\$\begingroup\$ you're right given that only one user is logged in! :) \$\endgroup\$Abel Tom– Abel Tom2017年01月25日 06:25:07 +00:00Commented Jan 25, 2017 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")
Previous answer :
system("shutdown/s")
DOS 7.1 (Windows 98) command prompt/batch, 5
WIN/P
Only works from real mode (MS-DOS command prompt).
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.
-
\$\begingroup\$ If using keypresses as a metric instead of program bytes, see this other answer for a shorter variant: codegolf.stackexchange.com/a/108248/38578 \$\endgroup\$Stéphane Gourichon– Stéphane Gourichon2017年01月27日 12:31:56 +00:00Commented Jan 27, 2017 at 12:31
Lisp, 38 bytes
(trivial-shell:shell-command "init 0")
-
\$\begingroup\$ Which dialect of Lisp does this work in? \$\endgroup\$ckjbgames– ckjbgames2017年02月13日 16:08:52 +00:00Commented Feb 13, 2017 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.
-
1\$\begingroup\$
open("/proc/sysrq-trigger","w").write("o")saves a few bytes. \$\endgroup\$Dennis– Dennis2017年01月28日 01:31:49 +00:00Commented Jan 28, 2017 at 1:31
JScript, 53 bytes
new ActiveXObject('WScript.Shell').Run("shutdown /s")
JScript is Microsoft's implementation of JavaScript. 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.
-
\$\begingroup\$ Would
os.system('shutdown\s')not work? I don't want to try it right now in case it does work :) \$\endgroup\$Farhan.K– Farhan.K2017年01月30日 11:16:46 +00:00Commented Jan 30, 2017 at 11:16 -
\$\begingroup\$ @Farhan.K No. It tries to find
sinside theshutdowndirectory. (Windows 10) \$\endgroup\$wizzwizz4– wizzwizz42017年02月17日 19:04:52 +00:00Commented Feb 17, 2017 at 19:04 -
1\$\begingroup\$ @wizzwizz4 Sorry I meant
os.system('shutdown/s')\$\endgroup\$Farhan.K– Farhan.K2017年02月20日 09:39:11 +00:00Commented Feb 20, 2017 at 9:39
Amstrad CPC Basic, 7 keypresses.
Amstrad CPC cannot be physically shut down by software, but system can be reset with this BASIC call:
call 0
How it works
General info
At the beginning of memory (either RAM or ROM, both are prepared for this), there is a table for Z80 RST instruction. These instructions, related to Z80 interrupt handling, are also used (even on machines other than Amstrad CPC, like TI-81) for other tricks because one-byte long assembly instruction is enough to reach any of the 8 entries.
On the Amstrad CPC they are used to space-efficiently call code in ROM, RAM, expansion RAM/ROM, see BIOS Function Summary - CPCWiki or ref page 8+. This is used extensively to make RAM-based indirections that allow easily calling ROM routines from RAM (even from BASIC, like call &bd19 that waits for screen refresh) and also, by changing them, modifying system behavior even though system is in ROM (like changing how text is displayed, or even redirecting it to printer by changing two bytes of RAM). See BB00 and following in BIOS Function Summary - CPCWiki or Firmware Guide
Specific use
Entry zero jumps to a ROM-base routine that performs full machine reset. The simplest way to call it from BASIC it call 0.
Variant
If you insist on a program that can be run rather than a direct command, it takes 8 keypresses:
1call 0
Then you can run.
Can I haz test?
You can test at once on CPCBox - Amstrad CPC emulator in Javascript.
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.
-
\$\begingroup\$ IIRC, this could be used to hang Win9x, where the DOS box had an IOPL of 3 \$\endgroup\$ninjalj– ninjalj2017年07月15日 16:17:00 +00:00Commented Jul 15, 2017 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}ḶṠȯƇọṣỴ\\Ḃ48ġṢṾ»Œ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
-
\$\begingroup\$ That's considerably more verbose than telling
app "Finder"to shut down. \$\endgroup\$Mark– Mark2018年09月11日 00:57:16 +00:00Commented Sep 11, 2018 at 0:57
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.
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.
ARM Thumb machine code, Nintendo DS, 30 bytes
05 48 05 49 00 23 02 88 12 0a fc d2 01 80 43 80
09 0c 40 23 f7 e7 c0 01 00 04 02 88 02 80
Thumb function. Runs at an address that is 2 byte aligned, 4 byte unaligned, on the ARM7. The ARM9 cannot access these registers.
This version follows the spec by writing the two halfwords separately, like the code in libnds. Known to work on DeSuMME (it will log it in the console menu) and melonDS, and should work on hardware.
Assembler source:
.syntax unified
.arch armv4t
.thumb
// Force misalignment to align PC load
// Not part of function
.p2align 2,0
.hword 0
// Begin code
// void shutdown(void);
.thumb_func
.globl shutdown
shutdown:
// Load address of SPI register
ldr r0, REG_SPICNT
// Load packed SPI commands
ldr r1, SPI_COMMANDS
// Set SPIDATA to select PM_CONTROL_REG
movs r3, #0x00
.Lloop:
// Wait for REG_SPICNT & SPI_BUSY to clear
ldrh r2, [r0]
// Test bit 7 by shifting into carry flag
lsrs r2, r2, #8
bcs .Lloop
// Store the low 16 bits of the command to REG_SPICNT
strh r1, [r0]
// Store data to REG_SPIDATA
strh r3, [r0, #2]
// Shift in the next SPI command
lsrs r1, r1, #16
// Set SPIDATA to PM_SYSTEM_PWR
movs r3, #0x40
// Run again with the new commands
// Then spin again until the SPI powers off the system.
b .Lloop
REG_SPICNT:
// Address of ARM7 IO register
.word 0x040001c0
// Two packed halfwords
SPI_COMMANDS:
// Start a command on the Power Management device,
// read a byte to select the register
// SPI_ENABLE | SPI_BAUD_1MHz | SPI_BYTE_MODE | SPI_CONTINUOUS | SPI_DEVICE_POWER
.hword 0x8802
// Finish the command on the Power Management device,
// read a byte to run the action.
// SPI_ENABLE | SPI_BAUD_1MHz | SPI_BYTE_MODE | SPI_DEVICE_POWER
.hword 0x8002
ARM Thumb machine code function, Nintendo DS, unsafe, 28 bytes
03 48 04 49 02 88 12 0a fc d2 01 60 02 49 f9 e7
c0 01 00 04 02 88 00 00 02 80 40 00
This function runs at a 4 byte aligned address on the ARM7.
This cheats 2 bytes by writing to SPICNT and SPIDATA at the same time in a full word write, something that has not yet been confirmed to work on hardware. It does not work on DeSuMME, but this might just be an emulator bug.
I am including this because it uses different code.
Assembler source:
.syntax unified
.arch armv4t
.thumb
// Force 4 byte alignment
.p2align 2,0
// Begin code
.thumb_func
.globl shutdown
shutdown:
// address of SPI register
ldr r0, REG_SPICNT
// Set SPI register bank
ldr r1, SPI_SELECT_PM_CONTROL_REG
.Lloop:
// Wait for REG_SPICNT & SPI_BUSY to clear
ldrh r2, [r0]
// Test bit 7 by shifting into carry flag
lsrs r2, #8
bcs .Lloop
// Store to both SPICNT and SPIDATA
str r1, [r0]
// Send SPI command
ldr r1, SPI_SEND_PM_SYSTEM_PWR
// Loop again with the new value.
b .Lloop
.p2align 2,0
REG_SPICNT:
.word 0x040001c0
// SPICNT = SPI_ENABLE | SPI_CONTINUOUS | SPI_BAUD_1MHz | SPI_DEVICE_POWER
// SPIDATA = PM_CONTROL_REG
SPI_SELECT_PM_CONTROL_REG:
// Combined halfwords
.hword 0x8802
.hword 0x0000
// SPICNT = SPI_ENABLE | SPI_BAUD_1MHz | SPI_DEVICE_POWER
// SPIDATA = PM_SYSTEM_PWR
SPI_SEND_PM_SYSTEM_PWR:
// Combined halfwords
.hword 0x8002
.hword 0x0040
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.
-
1\$\begingroup\$ You don't need to provide the shebang; just add 3 bytes for the flag in your title (something like
### AWK, 39 bytes + 3 (` -f`)\$\endgroup\$wizzwizz4– wizzwizz42017年01月24日 19:09:37 +00:00Commented Jan 24, 2017 at 19:09 -
\$\begingroup\$ Do you really have to use
poweroff? Wouldn'tinit 0orhalt -pwork? \$\endgroup\$statox– statox2017年01月25日 08:28:50 +00:00Commented Jan 25, 2017 at 8:28 -
\$\begingroup\$ @wizzwizz4 The request was for a program, so to make a standalone program requires the
#!and the-f. If I just wanted a script I would leave out that line entirely since:awk 'BEGIN{"poweroff"|getline}'works just fine. That would save me 13 bytes or so, but I wouldn't consider it aprogram. Just me being pedantic I guess. :) \$\endgroup\$Robert Benson– Robert Benson2017年01月25日 15:05:09 +00:00Commented Jan 25, 2017 at 15:05 -
1\$\begingroup\$ @RobertBenson I can't test it right now but given the bash answer I think you can save some bits. Indeed that's nice to have answer with
awkthat's not the first language which comes in mind when we think about codegolf :) \$\endgroup\$statox– statox2017年01月25日 15:35:14 +00:00Commented Jan 25, 2017 at 15:35 -
1\$\begingroup\$ @RobertBenson I didn't know about these differences of rights, that's good to know! \$\endgroup\$statox– statox2017年01月25日 17:03:23 +00:00Commented Jan 25, 2017 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 0ドルx2000037, %eax # b8 37 00 00 02
pushq 0ドルx08 # 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.
-
1\$\begingroup\$ Instead of
import subprocessyou could dofrom subprocess import*this way you don't spend as many bytes on the next line camping the subprocess module. \$\endgroup\$2017年01月29日 23:34:27 +00:00Commented Jan 29, 2017 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.
`whatever`;Bash/Perl/PHP/Ruby/etc. stupiglots. \$\endgroup\$shatdown: past tense of the verb shutdown\$\endgroup\$