This article coves multiple ways to perform a Linux system restart. We’ll be going over the steps to restart a Linux system from the terminal and also see a Python and C++ implementation of the same. The terminal is one of the most useful aspects of Linux as an operating system. From disconnecting networks to opening applications, Linux terminal does it all in a matter to seconds. In this article, we will be going through various ways to restarting the system, without the GUI.
Perform a Linux System Restart from the Terminal
There can be various commands that can achieve the task of restarting the system or scheduling a restart for the system. Let us take a look at some of the prominent ones.
1. Using the shutdown command
As the name suggests, shutdown
command can be used for managing the state of the system, that is, powering-off or rebooting. In order to reboot the system immediately, we run:
sudo shutdown -r now
The '-r'
option specifies the command to reboot instead of powering it off. The now
keyword notifies the command to proceed with the task immediately.
The shutdown
command can be used to schedule a restart in some minutes or hours. For instance, if we want to schedule a restart in 30 minutes, we run:
sudo shutdown -r +30
The above command specifies the exact time of execution and implements as soon as the time is finished.
After the command is scheduled and we do not wish to restart the system, we can overrule the command using the '-c'
flag:
sudo shutdown -c
To learn more about the shutdown
command, we can head over to the manual pages by typing man shutdown
in the terminal.
2. Using the reboot command
A system restart can be considered a soft reboot, where the Operating system turns off all the running and pending programs before shutting down the system. The reboot
command does a clean shutdown in the essence that it is similar to a normal system restart. We need to simply run:
sudo reboot
The reboot
command generally calls the systemctl
, which is a controlling command for System Manager. There can be a forceful reboot implemented using the '-f'
option
sudo reboot -f
The reboot
command is a part of the system state management trio. The other two are halt
and power-off
that perform their task represented by their names.
3. Using the telinit command
The Linux system has a concept of runlevels, which basically defines the list of scripts or daemons to be run for achieving a particular system state. There are a total of 7 runlevels. The runlevel 6 is reserved for a system reboot.
The telinit
command simply modifies the current runlevel to achieve a specific state for the system. In order to reboot the system, we run:
sudo telinit 6
The above command does the job of running specific scripts and other background processes to restart our system. There are manual pages for runlevel
as well as telinit
to acquire more knowledge on this topic.
Restarting a Linux System using Python
For the purpose of restarting a Linux using a Python script, we use the famous os module. It can easily send commands to the Linux terminal that can be executed as a generally typed command.
There can be few additions to the Python script for better user experience like a Main Menu and a Cancellation Question.
1. Main Menu
The Python script asks for the user’s choice, whether a power-off or a restart is requested.
import os
# The Main menu
print("\tMAIN MENU")
print("Enter P for Power-off")
print("Enter R for Restart")
print()
# User's choice
choice = input("Enter your choice: ")
2. Constructing the required command
After the user mentions his/her requirements, the appropriate command is to be constructed. That includes the amount of minutes the shutdown is to be scheduled.
# Some sanity checks
if len(choice) == 1 and (choice.upper() == 'P' or choice.upper() == 'R'):
# Command to be used eventually
command = "shutdown"
# User input for scheduling shutdown
try:
minutes = int(input("Enter number of minutes: "))
except ValueError:
print("Something wrong in input")
quit()
# Power-off command
if choice.upper() == 'P':
command += " -P" + " +" + str(minutes)
os.system(command)
# Reboot command
elif choice.upper() == 'R':
command += " -r" + " +" + str(minutes)
os.system(command)
else:
print("Something went wrong")
Firstly, the code does some sanity checks on the input and then it accepts an integer for the number of minutes the shutdown is to be delayed. After we have all the information, we use system()
function for relaying the command to the terminal for proper execution.
The key thing to note here is that, the Python script eventually performs the shutdown
command discussed previously.
3. Cancellation Request
At the end of the Python script, the user is asked whether the shutdown request is to be cancelled or not.
# Cancellation Question
choice = input("Do you want to cancel? (Y/N) : ")
# Cancelling the shutdown
if choice.upper() == 'Y':
os.system("shutdown -c")
If the user decides to cancel, the corresponding cancellation request is sent to be implemented.
4. Complete Python Code to Perform a Linux System Restart
import os
# The Main menu
print("\tMAIN MENU")
print("Enter P for Power-off")
print("Enter R for Restart")
print()
# User's choice
choice = input("Enter your choice: ")
# Some sanity checks
if len(choice) == 1 and (choice.upper() == 'P' or choice.upper() == 'R'):
# Command to be used eventually
command = "shutdown"
# User input for scheduling shutdown
try:
minutes = int(input("Enter number of minutes: "))
except ValueError:
print("Something wrong in input")
quit()
# Power-off command
if choice.upper() == 'P':
command += " -P" + " +" + str(minutes)
os.system(command)
# Reboot command
elif choice.upper() == 'R':
command += " -r" + " +" + str(minutes)
os.system(command)
else:
print("Something went wrong")
print()
# Cancellation Question
choice = input("Do you want to cancel? (Y/N) : ")
# Cancelling the shutdown
if choice.upper() == 'Y':
os.system("shutdown -c")
Restarting Linux using C++
The process of restarting Linux using C++ is almost similar to the above procedure. The added functions are explained below:
- system() – The C/C++ function used to send commands from code to the Linux terminal.
- c_str() – The function converts the string to char*, which is the required argument for system() function.
Apart from the above two functions, the C++ code follows the procedure used in the Python script.
Complete C++ Code
#include <iostream>
using namespace std;
int main(){
// The Main menu
cout<<"\tMAIN MENU"<<endl;
cout<<"Enter P for Power-off"<<endl;
cout<<"Enter R for Restart"<<endl;
cout<<endl;
cout<<"Enter your choice: ";
// User's choice
char choice;
cin>>choice;
// Some sanity checks
if (choice == 'P' or choice == 'R'){
// Command to be used eventually
string command = "shutdown";
// User input for scheduling shutdown
cout<<"Enter number of minutes: ";
string minutes;
cin>>minutes;
// Power-off command
if(choice == 'P'){
command += " -P";
command += " +";
command += minutes;
system(command.c_str());
}
// Reboot command
else if(choice == 'R'){
command += " -r";
command += " +";
command += minutes;
system(command.c_str());
}
else
cout<<"Something went wrong"<<endl;
cout<<endl;
// Cancellation Question
cout<<"Do you want to cancel? (Y/N) : ";
cin>>choice;
// Cancelling the shutdown
if(choice == 'Y')
system("shutdown -c");
}
return 1;
}
Conclusion
That brings us to the end of this article! Without a GUI, Linux allows us to perform a system restart in various ways that we’ve seen here today. Also, as a programmer working on Linux, you can use the ideas in the programming snippets here to implement in your code.
And lastly, if you wish to know more about any of the commands mentioned about, just type in man <command name>. The man command is the perfect documentation for Linux nerds.
We hope this article of restarting Linux via terminal was easy to follow. Thank you for reading.