Python Operating System and System

PythonPythonBeginner
Practice Now

Introduction

Welcome to the Python Operating System and System Lab, where we will transport you to the futuristic scenario of a space colony. In this advanced age, Earth has established a thriving space settlement on Mars, known as Ares Outpost. Your role is that of a pioneering cyber-farmer, a critical member of the colony responsible for managing the outpost's automated agricultural systems using Python.

The objective of this lab is to create robust Python scripts that interact with the operating system to ensure the smooth functioning of agricultural machinery and the underlying infrastructural software. You will accomplish tasks such as file manipulation, data logging, and system automation that are vital for the sustenance of the colony. Understand the importance of integrating Python with the operating system to achieve real-world objectives while making the scenario engaging.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") subgraph Lab Skills python/os_system -.-> lab-271578{{"`Python Operating System and System`"}} end

Setting up the Environment

In this step, you will set up your project environment which forms the foundation for our cyber-farming operations. Ensuring that directory structures are organized is essential for maintaining efficient workflows.

Open a Python script named ~/project/farm_operations/environment_check.py which will verify if all agricultural machinery interfaces are online. Here is a basic template:

import os

def check_interfaces():
    print("Checking machinery interfaces...")
    ## Example check (In reality, you would replace this with checks specific to your system)
    os.system("ping -c 1 127.0.0.1")

if __name__ == "__main__":
    check_interfaces()

Execute your script from the terminal:

python3 environment_check.py

You should see output indicating that the machinery interfaces are being checked, such as:

Checking machinery interfaces...
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.018 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.018/0.018/0.018/0.000 ms

System Monitoring

After verifying the interfaces, it’s essential to monitor the system's performance to prevent any disruptions in operations. For this, we will log important system metrics to ensure everything runs optimally.

Open a Python script named system_monitor.py within farm_operations:

import os

def system_monitor():
    print("Recording system metrics...")
    os.system("top -b -n 1 > system_metrics.log")

if __name__ == "__main__":
    system_monitor()

This script runs the top command in batch mode to collect system metrics and redirect them into a log file named system_metrics.log. Execute the script:

$ python3 system_monitor.py
Recording system metrics...

Check the contents of system_metrics.log to verify the successful logging of system data:

$ cat system_metrics.log
top - 00:33:14 up 15 days, 14:22,  0 users,  load average: 0.04, 0.07, 0.10
Tasks:  16 total,   1 running,  15 sleeping,   0 stopped,   0 zombie
%Cpu(s):  6.2 us,  0.0 sy,  0.0 ni, 93.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7802.7 total,    585.3 free,   3936.1 used,   3281.4 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   3555.6 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    216 labex     20   0  657480  56304  38872 S   6.7   0.7   0:00.63 node
      1 root      20   0   11200   3780   3508 S   0.0   0.0   0:00.02 init.sh
     21 root      20   0   40812  27976  10540 S   0.0   0.4   0:00.22 supervisord
     22 root      20   0   15420   9396   7760 S   0.0   0.1   0:00.01 sshd
     23 labex     20   0    2632    972    880 S   0.0   0.0   0:00.00 dumb-init
     24 labex     20   0  721668  63708  38596 S   0.0   0.8   0:00.56 node
     41 labex     20   0  951088 106276  41152 S   0.0   1.3   0:06.74 node
    167 labex     20   0  994340 134536  41504 S   0.0   1.7   0:07.99 node
    189 labex     20   0  848976  51504  38352 S   0.0   0.6   0:00.18 node
    233 labex     20   0   14392   6488   4604 S   0.0   0.1   0:00.37 zsh
    403 labex     20   0  377336  70216  11228 S   0.0   0.9   0:02.21 python
    430 labex     20   0   38268  25560   9832 S   0.0   0.3   0:00.17 python
    435 labex     20   0   14396   6588   4652 S   0.0   0.1   0:00.17 zsh
    863 labex     20   0   21156   9408   6076 S   0.0   0.1   0:00.01 python
    864 labex     20   0   11200   3652   3388 S   0.0   0.0   0:00.00 sh
    865 labex     20   0   14176   3576   3220 R   0.0   0.0   0:00.00 top

You will see an output similar to the top command within the terminal.

Summary

In this lab, we navigated through a scenario where Python's os_system module played a crucial role in administering a space colony's farming operations. Beginning with environment setup to system monitoring, the lab aimed to impart practical skills in creating scripts that manipulate and interact with the operating system. By holistically designing this lab, I emphasised the importance of Python in real-world applications and aimed to provide an engaging learning experience for beginners. Witnessing learners convert these scenarios into functioning code brings about a fulfilling sense of achievement.

Other Python Tutorials you may like