Total Pageviews

Translate

October 29, 2017

PowerHouse App for Ubuntu featuring Shutdown and Reboot functions

by 4hathacker  |  in Python at  3:40 PM

Hello everyone!

In the previous post, I have discussed about PyError Utility as a terminal based application. In this post, I will be discussing another simple application with some new libraries and a handful of knowledge about Linux.



Here we will be creating again a Linux Terminal application for simply rebooting or shutting down your system. The name of the application is 'Nitin's Powerhouse'. This powerhouse application will ask for some options  in order to reboot, shutdown or suspend your Linux System. If user is not root, it may ask for user password. 

I have used 'os' module in the  PyError Utility to run the Linux commands inside Python script. Here I will also use one more module to do the same. The name of the module is 'subprocess'. This module allows us to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. The methods of 'subprocess' module increase the flexibility and control as compared to 'os' modules.

Another library I have introduced is 'python-espeak'. It is a simple python wrapper for espeak speech synthesizer. It literally makes our computer to talk to us. There are a lot of methods for text-to-speech enhancement provided by this module. I have used only a single method 'espeak.synth()' to make the system read what is present on the screen. To install the same, run the following command:

pip install espeak python-espeak 

One beautiful aspect I added in this application is the colored font. I searched about how to change the colors of the font coming on terminal and came across a helpful link. Using 'tput' commands, it is possible to have colored fonts, foreground and background.





The next important thing I have used is 'init'. During booting of a Linux system, the very first process getting started is 'init'. 'init' is a daemon process that continues running until the system is shut down. It is the father of all the processes. It is started by the kernel using a hard-coded file name. 
 
In the code, 'init' is utilized with a runlevel to control the processes. A runlevel is a software configuration of the system which allows only a selected group of processes to exist. For quick response, I used 'init' as followed:

1. 'init 0' - To halt the system quickly and completely [Runlevel is 0]
2. 'init 6' - To reboot the system quickly [Runlevel is 6]

Other than that, I used normal 'shutdown' and 'reboot' commands to do the same and you can see the difference on your own running the application. You will find the complete code at my github account at this link.

Further to create a Desktop icon in Ubuntu, follow the steps mentioned.

1. Create a file with the same name but with '.desktop' extension. Here the script is 'power.py', so create a 'power.desktop' with vim editor.

2. Write in the file:
[Desktop Entry] 
Name=power
Exec=/path/to/power.py
Terminal=true
Type=Application

3. Exit with save pressing escape key, then ':wq' and hitting enter key

4. Make the 'power.desktop' file executable as:
chmod +x /path/to/power.desktop



This will make a power icon on Ubuntu Desktop. Double Clicking this icon, the Powerhouse App will appear in a new terminal as shown in below image.












0 comments:

Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.