Python
Give the computer steps it can repeat
Python is a language for writing instructions that a computer can run. A one-line command may be enough for a one-time task. Python helps when a task needs several steps, must make decisions, or works with information such as rows in a table, names in a list, or settings stored in a JSON file. A first Python instruction can be as simple as print("Hello, world!"), which asks Python to display a line of text.
A Python script is a text file containing instructions. The Python interpreter is the program that reads and runs that file. Scripts can group steps in functions, which are named pieces of work, and keep related information in collections such as lists or dictionaries. Python's standard library is a set of modules included with Python for common jobs such as reading paths, handling JSON data (a text format for structured information), parsing command-line options, and starting other programs.
Make inputs easy to change
A script that reads a log should let the person running it choose the log path, rather than requiring an edit to the source file. Command-line arguments are values supplied when the script starts. The argparse module can define and check those options; pathlib provides tools for working with file paths. Before reading the file, check that it exists and that the current account can read it.
Use the Python 3 interpreter documented for the system. A virtual environment is a separate place to install a project's Python packages without adding them to the base interpreter's package collection. Activate it during development or call its interpreter directly from an automated job. Operating systems can set rules for installing packages globally, so follow their packaging policy.
Python reports many failures as exceptions. An exception is information that an operation could not finish as requested. Handle it where the program can make a useful choice: explain the error, retry, or stop. Catching every exception and continuing can hide a partial failure.
Start another program safely
Python's subprocess module starts an external program and returns information about how it ran. This example asks systemd whether the sshd service is active, then prints the output and exit status:
import subprocess
result = subprocess.run(
["systemctl", "is-active", "sshd"],
text=True,
capture_output=True,
check=False,
)
print(result.stdout.strip() or result.stderr.strip())
print("exit status:", result.returncode)
The list gives the program and its separate arguments. With shell=False, the default, Python runs the program directly instead of asking a command shell to interpret a line of text. It passes each list item as a separate argument; characters such as spaces or * are not expanded by a shell. If check=True, a nonzero status raises an exception. With check=False, inspect returncode, the command's exit-status number, and handle the result yourself. stdout and stderr are the program's normal and error output.
With shell=True, Python starts a command shell, a program that reads and interprets commands. Quoting needs care: text supplied by a user could be interpreted as extra commands, a problem called command injection. Service names differ among distributions, and a successful systemctl result does not prove the application itself is healthy. Try normal input, a missing file, a permission error, and a command that exits unsuccessfully. Run the script as the same account and with an environment similar to its scheduled run. Keep secrets out of source and logs, and grant only the access the task needs.






