Shutdown and restart using commands

Many times we want to know how to turn off the computer, restart it ... each one after a certain time or at an exact time, well, here is how to do this from the terminal:

To shut down the PC:

kzkggaara @ geass: ~ $ sudo shutdown -h now
Note: It is necessary to enter our root password since administrative permissions are required.

To turn off the PC after a specified time:

kzkggaara @ geass: ~ $ sudo shutdown -h + "desired-time"
Must change ""Desired-time"”For the number or number of minutes to wait before shutting down the system.
Example: sudo shutdown -h +10 // The system will shut down 10 minutes after entering this command line.
Note: It is necessary to enter our root password since administrative permissions are required.

To turn off the PC at a specific time:

kzkggaara @ geass: ~ $ sudo shutdown -h "desired-time"
Must change ""Desired-time"”By logically the time they want the system to turn off. Clock in 24 hour format, that is; from 0 to 23.
Example: sudo shutdown -h 22:30 // The system will turn off at 22:30 p.m., that is; at 10:XNUMX at night.
Note: It is necessary to enter our root password since administrative permissions are required.

To restart the PC any of the following:

kzkggaara @ geass: ~ $ sudo shutdown -r now
kzkggaara @ geass: ~ $ sudo reboot
Note: It is necessary to enter our root password as administrative permissions are required. Also, either of the two previous lines do the same; restart the PC.

To restart the PC after a certain time:

kzkggaara @ geass: ~ $ sudo shutdown -r +"Desired-time"
Must change ""Desired-time" ”For the number or amount of minutes to wait before restarting the system.
Example: sudo shutdown -r +10 // The system will reboot 10 minutes after entering this command line.
Note: It is necessary to enter our root password since administrative permissions are required.

Related article:
Cron & crontab, explained

To restart the PC at a specific time:

kzkggaara @ geass: ~ $ sudo shutdown -r "Desired-time"
Must change ""Desired-time"”By logically the time they want the system to restart. Clock in 24 hour format, that is; from 0 to 23.
Example: sudo shutdown -r 22:30 // The system will restart at 22:30 p.m., that is; at 10:XNUMX at night.
Note: It is necessary to enter our root password since administrative permissions are required.

More commands in: Learn to do without the graphical environment


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   diazepam said

    I only use it when I need to restart the machine and I can't do it via graphical

  2.   fredy said

    Any idea to display a message every so often?

    1.    KZKG ^ Gaara said

      Explain better what you want to do and I help you achieve it 🙂

      1.    fredy said

        Let's say:

        shutdown -h 10 «shutting down the pc in» #time

        the idea that it displays a message within the same terminal every so often to warn the shutdown.

        1.    KZKG ^ Gaara said

          Ah, so simple.
          We first put it to wait 60 seconds, after those 60 seconds have elapsed, show a message on the terminal that says "Shutting down" and after 10 more seconds, start the shutdown process.
          It would be like this:
          sleep 60 && echo "Apagando" && sleep 10 && shutdown -n

          If you want to show a message but it is a system notification (Gnome or KDE) it would be with the notify-send command, for this to work you must have libnotify-bin installed, and the line would be:
          sleep 60 && notify-send "Apagando" && sleep 10 && shutdown -n

          1.    fredy said

            Thank you thank you, it's what I've been looking for in a long time.

    2.    Francisco Martinez said

      Hello, I think the information that I am going to offer you will help you, if you have doubts you can come to me;
      You can use the file (located in / etc /) / command «crontab» to create «a scheduled task» by entering the values: day of the month, day of the week, who executes it ...
      It is very useful for servers that require some autonomy ... I hope it helps 🙂

  3.   Kitty said

    Very good, but they are also:
    init 0 (turn off the pc)
    init 6 (restart the pc)

    They are easier hehee,
    regards!

    1.    KZKG ^ Gaara said

      And a bit suicidal I also think haha, because going directly to runlevel 0 without going through the others, is the equivalent of removing the power cable, right?

      init 0 what it does is that right? 🙂

      1.    Kitty said

        Hahaha yeah, I think when I'm in a hurry is when I do it xD

        1.    Courage said

          You'll see when you load your computer heh heh.

          Well if not, the elav carcamal gives you one because he likes you.

          1.    Kitty said

            Hahaha, how bad you are ^^
            Well, I won't do it again. Lo prometo!
            Regards!

            1.    Courage said

              But if I am a very good woman xD


      2.    ever said

        No, it is not suicidal. Levels 0 and 6 stop all services, unmount the drives before shutting down, so it is not the same as unplugging the cable.
        regards

        1.    KZKG ^ Gaara said

          I thought that the intermediate runlevels did this, and then the last one that was executed is the one that simply closed the connection ... come on, it turned off the equipment. From what I thought, that simply going to this runlevel the system would turn off without having dismounted everything, can you put a link for me to read about it? 🙂

          Thank you

          1.    ever said

            I don't know if it links, but I invite you to see the contents of the /etc/rc0.d and /etc/rc6.d directory (if you use Debian), and compare them with the other levels.
            There are all the dynamic links to the services that start and stop when switching from one run level to another. Links that start with the letter "S" start a demon, those that start with "K" stop them. As you can see, at levels 0 and 6, all links to daemons start with K, which means that when switching to those levels they stop all daemons, unmount the filesystems, and then the difference between 0 and 6 is that one sends the reboot signal and the other the poweroff signal.
            Surely a "man init" or similar will give you more information. From this pc that I write, I cannot show you or tell you well in which section it speaks because I am using systemd, which replaces all the entries in the SystemV manual, such as init.
            regards

    2.    Juventine said

      I do it this way ...
      (I learned it years ago from a SUN Microsystems system
      first login as "root", then I type:

      "Sync", then I press "enter"
      then I write:
      "Init 0" and then I press "enter" and with this I turn off the machine

      To restart I log in as "root" and type:
      "Sync", then "enter"
      then I write:
      "Reboot" and deposed I press "enter" and with this I restart the machine

      This works for me on a Debian system, I hope it helps you.

      1.    Cristian said

        How to turn on my laptop with a wireless keyboard?

  4.   Gregory Swords said

    Tip: Make shutdown-h now It is equivalent to halt (advantage: it is shorter, without parameters and easy to remember), according to the official documentation (man halt).

    1.    ever said

      The halt command doesn't work the same way if you use SystemD. In that case, it stops the entire PC, but it does not send the ACPI signal to shut down, so you will have to press the power button manually. Therefore, the recommended command to replace "shutdown -h now" is poweroff.
      regards

      1.    fedora user said

        eVeR what you say is not like that, Halt works in systemD, you just have to add the shutdown option:
        #halt -p
        the only drawback with Halt is that it requires root and / or sudo permissions
        I use Fedora, regards

  5.   Marco said

    Thanks for the information. very useful!!!

  6.   Merlin The Debianite said

    I admit that I did not know about restarting at a certain time but I really do not understand what use it would have at least in my case I only turn off the machine at a certain time when I stop downloading something and I calculate when it finishes and I always give it 1 or 2 hours longer than it lasts download.

    Good contribution.

  7.   Miguel said

    Very well, it must do something for something 🙂

    1.    e2391 said

      It is useful for those of us who use Window Managers. In this way you can create an entry in the menu (if you have one) to shut down or restart the PC.

      So that sudo does not ask for a password when shutdown or some other special command is executed, a rule can be created in / etc / sudoers =)

      Regards!

  8.   Rayonant said

    Useful for when I want to turn off the computer after a certain time, for downloads or similar, thanks for sharing.

    1.    KZKG ^ Gaara said

      Thanks to you for commenting

  9.   Jesus said

    One question, and how do I make the pc turn on at a certain time?

    1.    ever said

      You can't make it turn on from 0, but you can make it come back from a sleep. That's what the BIOS is for, or also in compatible BIOS there is a Linux tool. If you are interested, I will give you the link.
      regards

  10.   virgilius said

    You can use halt or poweroff to shut down and reboot to restart (they do not need parameters) with the advantage that they do not need administrative permissions and therefore we could, for example, put; cp largefile.mkv / media / device; poweroff

    Otherwise you have to put the password and we could not make it turn off after an operation unless we do it as root.

    1.    0N3R said

      We can add the -f parameter to force the computer to shutdown or restart almost instantly, regards.

      1.    ever said

        Forcing the shutdown is not good, since it executes the order to cut the power without stopping services or dismounting disks. That is why it is so fast!
        regards

    2.    ever said

      halt and poweroff DO require administrative permissions. Using the graphical interface is easier because X runs as root, so it doesn't ask for elevation

      1.    alvarito050506 said

        Not so, at least on Raspbian (Debian 9 Jessie), I run startx as normal user (pi).

  11.   jmvr1957 said

    A query, if I need to turn off at a certain time each day, for example at 22:30 p.m. every day of the year, how should I do from the terminal? Thanks a lot.

    1.    KZKG ^ Gaara said

      To do this, put the shutdown line or command in the crontab: shutdown -r 22:30 ... I recommend that you read this so that you learn how to use the crontab: https://blog.desdelinux.net/chuleta-para-entender-mejor-el-crontab/

  12.   dennis said

    Hello, I would like to know how to turn off the system in 8 seconds, because if it gives me, but in minutes I want in seconds

    1.    KZKG ^ Gaara said

      shutdown -t 8
      Sure, you need to run this as root or else using sudo

  13.   dennis said

    Hello I would like to know how to turn off the system in 8 seconds because I have been using some instructions but it only turns it off in minutes I want to know in seconds

  14.   dionny fernandez said

    I am making a script using these commands to turn off my machine at a certain time, everything is fine until the point of requesting the password arrives, my question is how to put in the code that I am root or that when I get to sudo su put the key automatically and execute it ???
    Thanks in advance for your answers…

    1.    Don Juan said

      you can have the shutdown command run by the user.
      Step by Step
      cd / sbin
      chmod u + s shutdown

      then you create a symbolic link in / usr / bin
      cd / usr / bin
      ln -s / sbin / shutdown shutdown
      and this is the script will work without the need for root

  15.   Don Juan said

    umm

  16.   ultron said

    What if I want to schedule my computer to turn on at a certain time?
    by the way, thanks I've been looking for this information for a long time, that is, about half an hour ago ... a long ... a long time.

  17.   ultron said

    Could you also help me? I am new to linux and I started with ubuntu but my career requires me to use Autocad how can I use Autocad in ubuntu? Would the Mac version of Autocad work?

  18.   Carlos Salas said

    Thank you !!! I was needing your information we are in contact from wherever you are if you are a girl a Muuuuuuuuah and if you are a boy forget it, heh heh.

  19.   Hector said

    Very useful information!

  20.   gabriel.uy said

    Hello one question. What is the command that allows me to send a message to all computers connected to a Wi-Fi network through a terminal ??? Thank you in advance!

  21.   Eduardo said

    Hello, very good article… The question is: how would I turn off every day at the same time obviously from the terminal… Thank you in advance.

    1.    Juan C.P. Quintana said

      Hello Eduardo! You could put it in the cron so that it runs when you need it.

  22.   Martin said

    In my laptop I have 2 partitions, in the first I have Windows 10 installed, in the second I have Debian 8.3 Jessie installed. When I have logged into Debian and then want to turn it off with the shutdown button or by console, it rarely shuts down, most of the time it doesn't shut down the system, on the contrary, it reboots and shows me the grub home screen to select the operating system I want to start. I don't know why this is. If anyone could help me, I would appreciate it. I am new to Debian. Cheers..

  23.   swifts said

    Can someone please help me. In my laptop I have 2 partitions, in the first I have Windows 10 installed, in the second I have Debian 8.3 Jessie installed. When I have logged into Debian and then I want to turn it off with the shutdown button or by console, once in a while it shuts down, most of the time the system does not shut down, on the contrary, it restarts and shows me the grub home screen to select the operating system that I want to start. I don't know why this is. If anyone could help me, I would appreciate it. I am new to Debian. Cheers..

  24.   savior said

    Hello I need help, if I want to make a menu with the two options shutdown and restart, but at least the first time I run the script if I give it shutdown, it should send me the question again, forcing to restart the system, at least the first time, after this turn it on again and run the script but this time it acts normal, that is, if I turn it off, turn it off and restart restart, how can I do it, that the machine remembers that I already force the restart at least once .

  25.   Diego said

    How can you build a script that executes the correct shutdown when it loses 5 pings in a row, and from then on the shutdown has a delay of 5 minutes, but during that time keep pinging, and if you have a response, cancel the shutdown and continue everything normal, and if it continues without response, turn off in that maximum time.
    It is because I have an unused router connected to the normal current, and when the light goes out it turns off and there it loses the ping, …… and the pc has UPS / UPS and I don't want it to turn off when the battery runs out. (so it does not download completely)

  26.   M @ rko said

    Hello good, I am new using Linux and I have Windows 10 and Linux deepin 15.3 on my laptop and I would like to schedule a power-up at a certain time, thanks

  27.   Drumsman ~ said

    I use Fedora. Just enough poweroff to shut down and reboot to restart. No elevation is required to run them.

  28.   abdiel49 said

    How do I solve a shutdown problem, what happens is that when I use any command to shut down or restart my machine it seems that it turns everything off but the disks, the processor continues to work so that I have no other option but to press the power button until it shuts down and that doesn't seem healthy for my team, thanks in advance.
    P.S.
    I am on a Debian 9 / Genom and I use an HP AMD A9 / Radeon R5 Grafics