Shut down the computer!

85

18

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!

P. Ktinos

Posted 2017-01-24T13:38:02.787

Reputation: 2 742

62I got friends, who do this in school and think they are 'hacking' :D – RaisingAgent – 2017-01-24T14:00:07.250

12We should come up with some rule once to stop such \whatever`;` Bash/Perl/PHP/Ruby/etc. stupiglots. – manatwork – 2017-01-24T14:22:01.590

2Full shutdown / Power off @TheBitByte – P. Ktinos – 2017-01-24T14:23:24.250

Okay, are programs that shutdown after a specific time, and not instantly, allowed? The batch answers shuts down after one minute. – Buffer Over Read – 2017-01-24T14:25:57.007

1@TheBitByte as long as the other rules are followed, it's allowed. – P. Ktinos – 2017-01-24T14:28:41.290

263Windows, 0 bytes. Leave the computer on for a few days and let automatic updates do their work – Luis Mendo – 2017-01-24T15:17:12.280

6@LuisMendo automatic updates do not shut down the machine, only restart. – briantist – 2017-01-24T15:26:11.250

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 – 2017-01-24T15:30:51.893

@NZKshatriya the rules say no, it must be powered off :-p – briantist – 2017-01-24T15:32:04.030

Crapola Well, that is one idea out the window. – NZKshatriya – 2017-01-24T15:33:41.970

5@briantist On my computer, restart is shutdown followed by power-on – Luis Mendo – 2017-01-24T15:37:17.490

Is it acceptable, if the computer shows a confirmation dialog before shutting down but after running the program? – insertusernamehere – 2017-01-24T15:47:59.890

can the code be a function or does it need to be a programm ? – HopefullyHelpful – 2017-01-24T16:10:29.227

28Anybody else notice the lack of TIO-links? – steenbergh – 2017-01-24T16:37:04.657

3http://notaverb.com/shutdown – Monty Harder – 2017-01-24T17:00:21.587

8@MontyHarder After reading that page I have decided to coin the term shatdown: past tense of the verb shutdown – FGreg – 2017-01-24T17:34:35.493

8@MontyHarder People nowadays will verb almost anything. – steenbergh – 2017-01-24T18:54:37.200

6

If the machine catches fire, does that count as a "shutdown"? ;-P

– Digital Trauma – 2017-01-24T19:28:18.437

1Does killing the computer by deleting all the data on it count as a shutdown. ie: running any invalid bash script on suicide linux – fəˈnɛtɪk – 2017-01-24T20:21:42.937

@LuisMendo Depends on your settings really. – Kamen Minkov – 2017-01-24T21:06:20.353

@LliwTelracs Resource exhaustion isnt allowed, and I guess important files for the OS are resources, that you "exhaust" by deleting.. or whatever. – P. Ktinos – 2017-01-24T21:40:08.980

1@P.Ktinos But you are freeing up more space by executing rm -rf / – fəˈnɛtɪk – 2017-01-24T21:52:51.817

7TI-BASIC, 2 bytes Note that this only works on an actual TI-84+, not an emulator. Works great on a TI-84+CE with low battery charge. :Pause Waits for enter keypress. Don't press enter and it will run out of batteries sooner or later. – Golden Ratio – 2017-01-25T00:24:56.940

1On systems that don't support shutdown, such as microcontrollers, can we reset or sleep? – v7d8dpo4 – 2017-01-25T08:26:38.947

2@steenbergh Had you read the link, you'd see it's not an objection to verbifying other parts of speech. It's about the fact that "shutdown" (as a single word) can't be a verb. Conjugate the alleged verb. Do I say "Every night before bedtime, steenbegh shutdowns the computer?" Would I say "The impasse between President and Congress shutdowned the Federal government for three days?" If you can see that these are wrong, you can see why the correct "phrasal" verb form is "shut down" as two words, not one. – Monty Harder – 2017-01-25T16:43:29.993

1If the code turns off the machine, it is considered a shutdown? – Margaret Bloom – 2017-01-25T19:45:52.007

@LuisMendo the rule requires the machine to be powered off, so the power on later is not allowed – phuclv – 2017-01-26T14:09:28.073

I thought about the opposite challenge, "write a code that turn on the computer". But, I guess that beside wake-on-lan and options in BIOS/UEFI, there isn't much options around… (Cf. this question on ubuntu).

– Clément – 2017-01-27T12:59:52.457

2Just waiting for someone to find a JavaScript exploit for this challenge. – Dan – 2017-01-31T10:22:30.637

PC tower before ATX, 1 keypress. On Power buton! – sergiol – 2017-02-18T12:56:59.043

@MontyHarder: dead link! – sergiol – 2017-05-20T19:30:48.947

I was about to answer "%0|%0 windows batch, 5 bytes" when I saw the rule about resource exhaustion. Bummer. – Hankrecords – 2017-06-06T07:12:32.290

Is "melting the CPU" considered to be "resource exhaustion"? – Bob Jarvis - Reinstate Monica – 2017-10-06T18:54:59.923

Answers

92

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.

Dennis

Posted 2017-01-24T13:38:02.787

Reputation: 196 637

Do we normally count assembly as compiled bytes without the framing bytes required for exe header? – Joshua – 2017-01-28T14:42:34.103

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. – Dennis – 2017-01-28T16:41:30.940

Could you add the hex dump after assembly of the code? – ckjbgames – 2017-02-13T16:05:05.327

@ckjbgames Here it is.

– Dennis – 2017-02-13T19:21:34.890

I fee1 so dead right now. – arminb – 2018-06-27T14:42:46.620

50

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)

briantist

Posted 2017-01-24T13:38:02.787

Reputation: 3 110

4Shorter one for Win7 & Win10 (and maybe more): gcm *p-c*|iex -- and there's only the one result too! – Bob – 2017-01-25T04:33:17.060

@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). – briantist – 2017-01-25T14:52:09.180

39

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.

steenbergh

Posted 2017-01-24T13:38:02.787

Reputation: 7 772

79Press the big button on the front of your computer = 0 bytes – RaisingAgent – 2017-01-24T13:53:17.597

97@RaisingAgent Here at PPCG we solve our problems in code. Physical labour is frowned upon. – steenbergh – 2017-01-24T13:54:36.350

1Is it possible for a shorter code to exist written in assembly or parallel language(s) ? – Suhrid Mulay – 2017-01-24T14:09:12.703

12@steenbergh I don't know... pressing one big button vs 10 keystrokes? I'll go with the button – WorldSEnder – 2017-01-24T21:01:15.697

17@RaisingAgent Your proposal has scalability issues. Try doing it for 10.000 computers four times a week. – Emilio M Bumachar – 2017-01-26T12:14:42.897

3@RaisingAgent Unfortunately is not always on the front :P – Alex Char – 2017-01-26T12:51:51.397

10@RaisingAgent real men use the switch that's right on the PSU – Cruncher – 2017-01-26T14:18:30.137

5(smashes computer with sledgehammer)...hunh...puny switch... :-) – Bob Jarvis - Reinstate Monica – 2017-01-26T19:03:16.280

3Pretty sure toggling a switch would at least count as 1 bit (or 0.125 bytes, if you prefer) – Nick T – 2017-01-26T19:57:35.950

2@RaisingAgent: and how do you press the big button when the box is on another continent? – ccpizza – 2017-01-27T12:26:54.540

2@ccpizza: You use an ICBM. Which, incidentally, also includes pressing a big button. – Tim Čas – 2017-01-29T17:06:14.037

1@Cruncher The one to switch from 230 V to that weird American 120 V? Works once only... – Tobias Kienzler – 2017-01-30T11:47:33.027

36

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.

Margaret Bloom

Posted 2017-01-24T13:38:02.787

Reputation: 1 946

13Upvoted because of the limited platform. – pipe – 2017-01-26T14:39:38.760

Ha! Somebody beat Dennis! – Joshua – 2017-01-30T16:57:20.670

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. – Jakob – 2018-06-26T19:03:08.983

@Jakob You are right! – Margaret Bloom – 2018-06-26T21:26:09.917

You look like the right person to answer this question: https://stackoverflow.com/questions/2019463/shutdown-computer-in-ms-dos-using-acpi (I failed miserably at it, had to delete my "answer" out of shame.)

– Prof. Falken contract breached – 2018-09-03T12:40:23.230

@Prof.Falken That question is quite old, any particular reason you would like to see it answered? I'm asking just because as it is right now it doesn't seems worth answering unless you can find some value in doing that. – Margaret Bloom – 2018-09-03T13:03:18.577

@MargaretBloom, no, not in particular - just as some kind of hobby. I have a thing for old and weird stuff, and combinations of old and new. DOS + ACPI hits just that note with me. Didn't mean to take your time, have a good day. :-) Looking at it holistically, answering that question may well represent an opportunity cost with a net negative. I.e. you might have better stuff to do... – Prof. Falken contract breached – 2018-09-04T08:36:57.790

@Prof.Falken More than "better" I'd say mandatory stuff do to :P Like everyone else. Well, if I can find the time I'll be glad to answer that question and ping you back :) – Margaret Bloom – 2018-09-04T11:58:10.067

34

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

zeppelin

Posted 2017-01-24T13:38:02.787

Reputation: 7 884

grub shell is not a "programming language" - it's not Turing complete – Alnitak – 2017-01-25T11:38:34.320

@Alnitak really? Even though you can load lua as a module? But note this code also works in bash. – abligh – 2017-01-25T12:14:00.257

10

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

– zeppelin – 2017-01-25T12:17:22.017

1OK, I'll take that back - I didn't find that stuff when I looked – Alnitak – 2017-01-25T12:32:39.573

@abligh It doesn't work in bash since the bash version doesn't actually power off the machine without the -p – Suever – 2017-01-25T15:48:02.963

29When it cannot display a spinner it is obviously not Turning-complete – J_F_B_M – 2017-01-26T11:31:45.567

@J_F_B_M ha ha :) – zeppelin – 2017-01-26T14:02:24.063

@J_F_B_M what about those spinners which go like: _ \ | / _ | \ | ... etc – Restioson – 2017-01-29T11:27:25.977

1In Bash it's the same command. – Paweł Tokarz – 2017-01-30T10:55:47.503

32

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                                       .

2xsaiko

Posted 2017-01-24T13:38:02.787

Reputation: 699

Noice. I was an Apple II hacker back in the day and didn't even remember this opcode. – kindall – 2017-01-25T19:06:08.060

2Yeah, 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/ – 2xsaiko – 2017-01-25T19:17:35.373

25

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.

Stewie Griffin

Posted 2017-01-24T13:38:02.787

Reputation: 43 471

106"I'll add a short explanation later, my laptop is currently off." <- The danger of a shutdown challenge – Suever – 2017-01-24T14:08:13.700

4Axaxaxaxa, well, restarting your computer every once in a while can't be bad :) – RaisingAgent – 2017-01-24T14:14:25.177

6Does ruin your uptime, though – Wayne Werner – 2017-01-26T19:02:23.443

Why submit this as MATLAB, instead of submitting shutdown -s as shell? – user253751 – 2017-01-27T05:50:16.867

10Because for me, shell is either something I find on the beach, or a gas station. – Stewie Griffin – 2017-01-27T07:18:26.327

21

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.

Sanchises

Posted 2017-01-24T13:38:02.787

Reputation: 8 530

19

SmileBASIC, 14 11 6 bytes

OPTION

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

Works in the most recent version.

12Me21

Posted 2017-01-24T13:38:02.787

Reputation: 6 110

I thought this had been patched... Please include a version number. – wizzwizz4 – 2017-01-24T19:05:21.450

3It has been patched in version 3.5, which hasn't been released outside of Japan yet. – 12Me21 – 2017-01-24T19:35:54.577

4@12Me21 Sure it counts. Nice choice of environment. – P. Ktinos – 2017-01-24T21:46:15.470

If only REBOOT from SBv2 counted as a valid answer. – snail_ – 2018-06-27T04:09:48.487

16

Bash, 7 6 bytes

init 0

Assuming that the program is run as root.

Suever

Posted 2017-01-24T13:38:02.787

Reputation: 10 257

Indeed, powering off the machine is required. Thus you need 'halt -p' as a valid answer – P. Ktinos – 2017-01-24T14:07:00.127

4This editing massacre :) – RaisingAgent – 2017-01-24T14:08:13.090

@P.Ktinos: poweroff is a single word, although slightly more letters. – ccpizza – 2017-01-27T12:32:37.473

1

@ccpizza Right, but the typical scoring criteria is byte count

– Suever – 2017-01-27T14:45:01.210

Also, init 6 would restart the machine, but not sure that would completely count as "turning off." – Chipster – 2019-11-02T05:42:51.250

14

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

Gurupad Mamadapur

Posted 2017-01-24T13:38:02.787

Reputation: 1 791

2

Someone else posted the Linux shell version.

– jpmc26 – 2017-01-25T01:34:05.310

1Ah ok, my bad.. – FlipTack – 2017-01-26T19:59:38.777

13

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.

Tom

Posted 2017-01-24T13:38:02.787

Reputation: 3 078

2It's shorter to replace class with interface as that allows you to omit public from main. – Pavel – 2017-01-24T15:42:39.507

6You 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");} – None – 2017-01-24T18:12:31.550

1

Wouldn't the Linux version be a bit shorter?

– jpmc26 – 2017-01-25T01:32:59.233

2()->{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. – Olivier Grégoire – 2017-01-26T12:23:27.087

1This should be labeled as a Linux-specific answer, e.g. "Java on LInux". – Jakob – 2018-06-26T19:05:58.760

13

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

Shaun Bebbers

Posted 2017-01-24T13:38:02.787

Reputation: 1 814

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 – 2017-01-24T17:03:57.553

2The 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 – 2017-01-24T18:24:22.280

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 – 2017-01-24T18:32:39.523

3You 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 – 2017-01-24T22:46:15.153

I used the PRINT FRE() function to determine the number of bytes used. Even if I obfuscate the BASIC 2.0 keywords, the FRE() function will still return the same number of bytes, and missing out the space between the 0 and the keyword will still produce the same result as Commodore BASIC adds in a space after the line number for presentation purposes. – Shaun Bebbers – 2017-01-25T09:21:21.790

2Does not power off. – TecBrat – 2017-01-27T15:03:41.860

1I once spilled beer on my Commodore 64, that powered it off permanently. Does this count? – Shaun Bebbers – 2017-01-27T15:06:52.380

12

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 ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$

simon

Posted 2017-01-24T13:38:02.787

Reputation: 351

1Now i am curious about IncludeOS – Karl Napf – 2017-01-25T19:57:36.077

@KarlNapf Alfred held a great presentation at CppCon 2016 about IncludeOS: https://www.youtube.com/watch?v=t4etEwG2_LY if you want to know more :-)

– simon – 2017-01-25T20:05:12.923

1

You don't need to use void Service::start, int main works in IncludeOS as well.

– Ingve – 2017-01-27T10:56:11.787

12

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

devRicher

Posted 2017-01-24T13:38:02.787

Reputation: 1 609

Isn't that 7 bytes? – Roman Gräf – 2017-01-26T14:52:55.133

http://meta.codegolf.stackexchange.com/a/4764/62824 @RomanGräf – devRicher – 2017-01-26T14:55:41.557

Sounds convincing to me. – Roman Gräf – 2017-01-26T14:58:28.030

10

AppleScript, 30 bytes

tell app "Finder" to shut down

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

Mark

Posted 2017-01-24T13:38:02.787

Reputation: 2 099

9

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.

ATaco

Posted 2017-01-24T13:38:02.787

Reputation: 7 898

1AHK is mysterious. – Gurupad Mamadapur – 2017-01-25T14:35:46.330

Do you need the space after the comma? – 2xsaiko – 2017-01-26T15:34:13.343

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. – Gurupad Mamadapur – 2017-01-26T15:52:24.173

9

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

Ruslan

Posted 2017-01-24T13:38:02.787

Reputation: 1 283

7

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");

Dariusz Woźniak

Posted 2017-01-24T13:38:02.787

Reputation: 1 131

2Note 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. – TheLethalCoder – 2017-01-25T14:18:45.607

@TheLethalCoder, updated, thanks! – Dariusz Woźniak – 2017-01-25T15:57:47.937

You can also remove the unnecessary space. – nothrow – 2017-01-27T20:59:18.717

Of course yes! Thank you, @nothrow :) – Dariusz Woźniak – 2017-01-27T22:38:32.220

6

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

rahnema1

Posted 2017-01-24T13:38:02.787

Reputation: 5 435

10 (logoff) is a special case here; it does not combine with Standby or Hibernate implicitly. – wizzwizz4 – 2017-01-24T19:02:38.357

OK 0 + 32 = 32 – rahnema1 – 2017-01-24T19:25:23.210

Hm, so does it always log off before going to standby/hibernation? – Ruslan – 2017-01-29T06:30:57.827

@Ruslan I do not know exactly how it works, You can refer to AutoIt user forum to receive more feedbacks:-)

– rahnema1 – 2017-01-29T07:05:56.163

6

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!

MrPaulch

Posted 2017-01-24T13:38:02.787

Reputation: 771

5

Groovy, 23 bytes

'shutdown /p'.execute()

Gurupad Mamadapur

Posted 2017-01-24T13:38:02.787

Reputation: 1 791

2Is the space before the slash necessary? – steenbergh – 2017-01-24T14:12:20.197

3@steenbergh Yes, it is needed. – Gurupad Mamadapur – 2017-01-24T14:32:57.367

@GurupadMamadapur are you sure? – TrojanByAccident – 2017-01-24T19:48:13.117

5

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

Daniel

Posted 2017-01-24T13:38:02.787

Reputation: 6 425

Someone came up with a shorter solution (with same program) a few hours after you (but posted it instead of commenting here) http://codegolf.stackexchange.com/a/107958/62824

– devRicher – 2017-01-25T11:08:54.713

You can probably remove the indentation – Cyoce – 2017-01-25T15:52:18.040

4tell app "Finder" to shut down should work, I believe (though the compilation may replace 'app' with 'application'). – kindall – 2017-01-25T19:04:11.063

5

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.

Radovan Garabík

Posted 2017-01-24T13:38:02.787

Reputation: 437

1Last time I tried wildcard expansion on redirect it generated a file with the wildcard in it despite a matching file existing. – Joshua – 2017-01-25T18:57:08.417

5I believe you have to add "/proc/" to your byte count – zeppelin – 2017-01-25T20:49:17.110

@Joshua It might be shell dependent. I actually tested it (with bash) on my computer :-) – Radovan Garabík – 2017-01-25T21:12:12.123

@zeppelin Not if you are already in the directory. For all we know, it might be your $HOME - unusual, but not impossible... – Radovan Garabík – 2017-01-26T14:17:57.477

1@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. – zeppelin – 2017-01-26T14:52:12.663

1But you can probably make it valid at expense of just 3 bytes, like this echo o>/p*/*ger – zeppelin – 2017-01-26T15:40:21.513

@zeppelin True. Anyway, going for the shortest command was not my primary goal. My goal was to have a command that does what has been asked, nothing more - other answers have the OS perform various side effects, like killing processes or umounting filesystems :-) – Radovan Garabík – 2017-01-27T08:17:03.797

5

Android shell, 9 bytes

reboot -p

Pedro Lobito

Posted 2017-01-24T13:38:02.787

Reputation: 170

7Welcome to PPCG! – ETHproductions – 2017-01-29T20:46:51.793

4

J, 14 bytes

2!:0'poweroff'

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

miles

Posted 2017-01-24T13:38:02.787

Reputation: 15 654

4

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

Carcigenicate

Posted 2017-01-24T13:38:02.787

Reputation: 3 295

2

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

fəˈnɛtɪk

Posted 2017-01-24T13:38:02.787

Reputation: 4 166

do you need the ;? – RaisingAgent – 2017-01-24T14:12:58.023

1@LliwTelracs Pretty sure it is "shutdown /p" and not "-p" and the space is not needed. – P. Ktinos – 2017-01-24T14:14:36.297

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 – fəˈnɛtɪk – 2017-01-24T14:21:54.457

@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 – 2017-01-24T14:25:54.047

I separated it into windows and linux power off – fəˈnɛtɪk – 2017-01-24T14:31:55.617

@P.Ktinos shutdown /p and shutdown -p are the same thing – TrojanByAccident – 2017-01-24T16:54:30.687

@TrojanByAccident What he meant is that i could remove the space if i used slash for on windows, which removed 1 byte – fəˈnɛtɪk – 2017-01-24T17:23:41.347

1#include<cstdlib> will shave off a byte. – None – 2017-01-24T18:14:00.553

2

Node.js, 39 bytes

Linux

require('child_process').exec('init 0')

Tom

Posted 2017-01-24T13:38:02.787

Reputation: 3 078

2

Bash 15 bytes

shutdown -h now

alternatively : init 0

Abel Tom

Posted 2017-01-24T13:38:02.787

Reputation: 1 150

1useless use of sudo – Ipor Sircer – 2017-01-24T22:03:33.757

you're right given that only one user is logged in! :) – Abel Tom – 2017-01-25T06:25:07.093

2

Mathematica on Windows 16 Bytes

Invoke the shell command with Run.

Run@"Shutdown/p"

Kelly Lowder

Posted 2017-01-24T13:38:02.787

Reputation: 3 225

Does this work on every OS, or a specific one? – Greg Martin – 2017-01-24T18:18:03.470

@GregMartin Windows only. Maybe a Linux guy can comment on modifying this to pass the appropriate shell command there. – Kelly Lowder – 2017-01-24T18:47:17.137

1I'm sure a Linux person of any gender could! Perhaps "Mathematica on Windows" is more precise in the post itself? – Greg Martin – 2017-01-24T20:20:58.047

3

Lol. I use "guy" in the gender-neutral sense. http://www.slate.com/blogs/lexicon_valley/2016/02/10/the_gender_neutral_use_of_guys_is_on_the_rise_but_it_s_a_slow_rise.html

– Kelly Lowder – 2017-01-24T22:56:01.610

1Although 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 – 2017-01-25T01:04:11.977

1Linux shutdown code: shutdown +0 is the traditional way to do a shutdown and power off (using the tersest representations of the flags); shutdown with no arguments will delay 60 seconds then shut down. On recent systemd-based systems you're supposed to use systemctl halt or systemctl poweroff instead, but that's more bytes so who cares about that. Note that any of these commands require root permissions (as shutting down the system could cause data loss for anyone else logged in at the time, it's an action that affects other users, so you need enough permissions to be able to do that). – None – 2017-01-25T06:08:49.473

2

AHK, 19 15 bytes

Run,Shutdown -s

Works on windows only.

Gurupad Mamadapur

Posted 2017-01-24T13:38:02.787

Reputation: 1 791

2

R, 20 19 bytes

shell("shutdown/s")

Previous answer :

system("shutdown/s")

Frédéric

Posted 2017-01-24T13:38:02.787

Reputation: 2 059

1shell("shutdown/s")? (19 bytes) – djhurio – 2017-01-25T14:33:28.563

@djhurio well done ! Thanks ! – Frédéric – 2017-01-25T17:37:35.230

2

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.

Timtech

Posted 2017-01-24T13:38:02.787

Reputation: 12 038

2

PHP, 20 bytes

shell_exec("init 0");

harkirat1892

Posted 2017-01-24T13:38:02.787

Reputation: 121

2

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

WIN/P

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

Joshua

Posted 2017-01-24T13:38:02.787

Reputation: 3 043

Isn't the switch for soft-power-off /Z? – MrPaulch – 2017-01-27T12:28:18.907

Might be. It's been a long time. – Joshua – 2017-01-27T16:18:46.987

2

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.

Stéphane Gourichon

Posted 2017-01-24T13:38:02.787

Reputation: 131

If using keypresses as a metric instead of program bytes, see this other answer for a shorter variant: http://codegolf.stackexchange.com/a/108248/38578

– Stéphane Gourichon – 2017-01-27T12:31:56.773

2

Lisp, 38 bytes

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

Jefrey Sobreira Santos

Posted 2017-01-24T13:38:02.787

Reputation: 121

Which dialect of Lisp does this work in? – ckjbgames – 2017-02-13T16:08:52.530

2

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.

Sergiy Kolodyazhnyy

Posted 2017-01-24T13:38:02.787

Reputation: 331

1open("/proc/sysrq-trigger","w").write("o") saves a few bytes. – Dennis – 2017-01-28T01:31:49.023

2

EthOS Shell - 1 byte

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

r

Pedro Lobito

Posted 2017-01-24T13:38:02.787

Reputation: 170

1

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.

MildCorma

Posted 2017-01-24T13:38:02.787

Reputation: 39

Would os.system('shutdown\s') not work? I don't want to try it right now in case it does work :) – Farhan.K – 2017-01-30T11:16:46.583

@Farhan.K No. It tries to find s inside the shutdown directory. (Windows 10) – wizzwizz4 – 2017-02-17T19:04:52.967

1@wizzwizz4 Sorry I meant os.system('shutdown/s') – Farhan.K – 2017-02-20T09:39:11.933

1

JScript, 53 bytes

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

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

user2428118

Posted 2017-01-24T13:38:02.787

Reputation: 2 000

1

VBScript, 46 bytes

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

Run with wscript scriptname.js.

user2428118

Posted 2017-01-24T13:38:02.787

Reputation: 2 000

1

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.

Stéphane Gourichon

Posted 2017-01-24T13:38:02.787

Reputation: 131

1

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

Shaun Bebbers

Posted 2017-01-24T13:38:02.787

Reputation: 1 814

1

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.

Ruslan

Posted 2017-01-24T13:38:02.787

Reputation: 1 283

IIRC, this could be used to hang Win9x, where the DOS box had an IOPL of 3 – ninjalj – 2017-07-15T16:17:00.857

1

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

`halt`

Requires you to be root.

tucker twomey CodeZypher

Posted 2017-01-24T13:38:02.787

Reputation: 11

1

Jelly, 25 bytes

It makes use of the undocumented monad ŒV, which takes a list, concatenates it and evaluates it using Python's eval.

“WƒuUḤøMŒƁEḄV#ẆṄⱮṁƬṅȯV»ŒV

The compressed string translates to import os;os.system("poweroff") so it only works on Linux (with root). The following code was used for compressing.

Compress().dictionary('import')
          .string(' os;os.')
          .dictionary('system')
          .string('("')
          .dictionary('power')
          .dictionary('off')
          .string('")').go()

By changing the poweroff it can be extended to other systems (but power and off are in the dictionary so this will probably be the shortest). For example, following 33-byte version worked on a Mac.

“¡Ṁ\\meṾṭ£ȧ+⁾⁸}ḶṠȯƇọṣỴ\\Ḃ⁴8ġṢṾ»ŒV

PidgeyUsedGust

Posted 2017-01-24T13:38:02.787

Reputation: 631

1

shortC, 10 bytes

A$shutdown

Explanation/ungolfed:

A           main function
 $          execute system command
  shutdown  shutdown

MD XF

Posted 2017-01-24T13:38:02.787

Reputation: 11 605

1

AppleScript (45 bytes)

tell application "System Events" to shut down

Dimitrie-Toma Furdui

Posted 2017-01-24T13:38:02.787

Reputation: 139

That's considerably more verbose than telling app "Finder" to shut down. – Mark – 2018-09-11T00:57:16.520

1

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.

Emiliano Ruiz

Posted 2017-01-24T13:38:02.787

Reputation: 11

0

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.

Robert Benson

Posted 2017-01-24T13:38:02.787

Reputation: 1 339

1You 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 – 2017-01-24T19:09:37.237

Do you really have to use poweroff? Wouldn't init 0 or halt -p work? – statox – 2017-01-25T08:28:50.470

@wizzwizz4 The request was for a program, so to make a standalone program requires the #! and the -f. If I just wanted a script I would leave out that line entirely since: awk 'BEGIN{"poweroff"|getline}' works just fine. That would save me 13 bytes or so, but I wouldn't consider it a program. Just me being pedantic I guess. :) – Robert Benson – 2017-01-25T15:05:09.320

@statox You are very probably right. I can't actually run those commands on my work machine, so I was writing from memory. As I recall on my machines init 0 often stops the kernel but does not turn off the machine, and I didn't recall halt -p. So I could save 1 or 2 bytes using those commands. I know this is code-golf, but I mostly write these to make sure that AWK is represented in the solutions. :) – Robert Benson – 2017-01-25T15:13:41.587

1

@RobertBenson I can't test it right now but given the bash answer I think you can save some bits. Indeed that's nice to have answer with awk that's not the first language which comes in mind when we think about codegolf :)

– statox – 2017-01-25T15:35:14.990

@statox I know this is beyond the requirements, but poweroff often doesn't require root access, whereas the other options do. I'm not sure why it doesn't. Maybe I'll re-write this to show the various options, but I should be getting back to working right now :p – Robert Benson – 2017-01-25T17:02:10.967

1@RobertBenson I didn't know about these differences of rights, that's good to know! – statox – 2017-01-25T17:03:23.303

@RobertBenson On this site, you're allowed to specify command line arguments used to run the program. So, for example, you'd run the script using /path/to/awk -f filename; that takes 3 bytes more than usual. However, I realise that the -f is only so a file can be provided, so you can completely miss out that line entirely like other AWK programs on the site do. It's not being lazy; it's losing every byte you can. – wizzwizz4 – 2017-02-17T19:02:30.220

0

C, macOS, 18 bytes

main(){reboot(8);}

This is not a proper shutdown. Usual caveats apply. On my MacBook Air, this resulted in a black screen with a spinning indicator, then a shutdown after a few seconds.

x64 Assembly, macOS, 11 bytes (untested)

From disassembling the reboot function in libSystem:

movl    $0x2000037, %eax   # b8 37 00 00 02
pushq   $0x08              # 6a 08
popq    %r10               # 41 5a
syscall                    # 0f 05

nwellnhof

Posted 2017-01-24T13:38:02.787

Reputation: 10 037

0

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.

tale852150

Posted 2017-01-24T13:38:02.787

Reputation: 111

0

AutoIt (windows), 11 bytes:

Shutdown(1)

Pedro Lobito

Posted 2017-01-24T13:38:02.787

Reputation: 170

0

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.

XtremeCODE

Posted 2017-01-24T13:38:02.787

Reputation: 1

1Instead of import subprocess you could do from subprocess import* this way you don't spend as many bytes on the next line camping the subprocess module. – Post Rock Garf Hunter – 2017-01-29T23:34:27.587

0

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

Taylor Scott

Posted 2017-01-24T13:38:02.787

Reputation: 6 709

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.

mroman

Posted 2017-01-24T13:38:02.787

Reputation: 1 382

0

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.

JohnZoidberg

Posted 2017-01-24T13:38:02.787

Reputation: 1

0

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

’À…„‹/p’.E

Try it online without the .E.

Explanation:

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

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

05AB1E, 25 bytes

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

Try it online without the .E.

Explanation:

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

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

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

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

Kevin Cruijssen

Posted 2017-01-24T13:38:02.787

Reputation: 67 575