Skip to content
Related Articles

Related Articles

Program to shutdown a system

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 23 Jun, 2022

How to shutdown your computer in Linux and/or Windows?

The idea is to use system() in C. This function is used to invoke operating system commands from C program. 

Linux OS: 

C




// C program to shutdown in Linux
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   // Running Linux OS command using system
   system("shutdown -P now");
   
   return 0;
}

Java




//Java program to shut down the system with linux OS
import java.io.IOException;
import java.util.Scanner;
public class Shutdown_System
{
    public static void main(String args[]) throws IOException
    {
        int sec=1;
        String operatingSystem = System.getProperty("os.name");
        System.out.println("Name of Operating System:"+operatingSystem);
        if(operatingSystem.equals("Linux"))
        {
            Runtime runtime = Runtime.getRuntime();
            Scanner s = new Scanner(System.in);
            System.out.print("System will shut down after 1 second:");
            Process proc = runtime.exec("shutdown -h -t "+sec);
            System.exit(0);
        }
        else
        {
            System.out.println("Something went wrong.");
        }
    }
}

 Windows OS:  Shutdown/ Log off/ Restart a Windows OS

We will make use of system() from < stdlib.h > to perform a system operation with the help of a C program. To perform any of the afore-mentioned system operation code will be as follows: 

C




//C program to shut down the system in Windows OS
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
  system("c:\\windows\\system32\\shutdown /i");
  return 0;
}

Java




//Java program to shutdoen the system after 5 seconds for windows OS
 
import java.io.*;
 
public class GFG
{
   public static void main(String[] args)
   {
      Runtime runtime = Runtime.getRuntime();
      try
      {
         System.out.println("System will shutdown after 5 seconds.");
         runtime.exec("shutdown -s -t 5");
      }
      catch(IOException e)
      {
         System.out.println("Exception: " +e);
      }
   }
}

The argument to the system function is the path to OS and /i is one of the entity from the vast options available to us. To view the options, we run cmd and type:

C:\Users\User>shutdown

The shutdown command presents us with a list of options available for us. 

These are : To perform different operations, we just replace the last “/path” in system() argument. The common operations are:

Shutdown

system("c:\\windows\\system32\\shutdown /s");

Restart

system("c:\\windows\\system32\\shutdown /r");

Logoff

system("c:\\windows\\system32\\shutdown /l");

Time Complexity: O(1), As program will directly execute the command  through OS, time complexity will be O(1).

Space Complexity: O(1)

This article is contributed by Sahil Chhabra and Amartya Ranjan Saikia. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!