With just a few simple tricks, you can automate tasks, speed up your workflow, and fully control your system without endless manual work. And the best part? You don’t have to be a Python expert to use these tricks.
Whether you’re just starting out or you’ve been using Linux for years, these tips will help you work more efficiently and get things done more easily.
So, let’s dive in and see how Python can supercharge your Linux workflow.
Running Multiple Python Scripts
Sometimes, one Python script isn’t enough. Maybe you’re running a web server while processing data, or you have multiple automation tasks that need to work in parallel.
Instead of running scripts one by one and waiting for each to finish, you can run multiple scripts at the same time to save time and maximize efficiency.
The easiest way to run multiple Python scripts simultaneously is by using the & operator.
python3 script1.py & python3 script2.py &
This command will run both scripts at the same time. The & tells Linux to run the command in the background so the terminal stays free. You can keep running other commands while the scripts are running.
Run Python Scripts in the Background
If you want to run a Python script without keeping the terminal open, use nohup or &.
nohup python3 script1.py &
nohup python3 script2.py &
The nohup command lets you run scripts that keep running even if you close the terminal.
Run Scripts Sequentially
If you need to run one script after another finishes, just use &&.
python3 script1.py && python3 script2.py
This command will start script1.py first. Once it completes successfully, script2.py starts. If script1.py fails, script2.py won’t run.
Check Running Processes
If you want to see if the scripts are running, use:
ps aux | grep python
If you need to stop them, use:
pkill -f script1.py
This will kill only script1.py, leaving the other running.
Run a Python Script on Startup
You can add your script to run automatically when Linux starts.
Create a service file:
sudo nano /etc/systemd/system/myscript.service
Add this:
[Unit]
Description=My Python Script
After=network.target
[Service]
ExecStart=/usr/bin/python3 /path/to/script.py
Restart=always
User=root
[Install]
WantedBy=multi-user.target
Enable the service:
sudo systemctl enable myscript
sudo systemctl start myscript
Now, your script runs automatically after a reboot.
Rename All Files in a Folder
If you want to rename every file in a folder, use the os module.
import os
folder = "/path/to/your/folder"
for filename in os.listdir(folder):
old_path = os.path.join(folder, filename)
new_path = os.path.join(folder, "new_" + filename)
os.rename(old_path, new_path)
print("All files renamed successfully!")
This code will search the folder for all files and rename them by adding “new_” before the original name. You can change the “new” to whatever you want.
Extract Errors from Huge Log Files
Most log files contain keywords like ERROR, WARNING, or FAILED. A simple way to find them is by scanning the file:
log_file = "server.log"
output_file = "errors.log"
with open(log_file, "r") as file, open(output_file, "w") as error_file:
for line in file:
if "ERROR" in line: # Check if "ERROR" is in the line
error_file.write(line) # Save it to errors.log
This code reads the server.log line by line, extracts lines containing “ERROR,” and saves them to the errors.log file.
Extract Errors from a Specific Time Range
Let’s say you want only errors that happened between 14:00 and 16:00 on a given day.
log_file = "server.log"
output_file = "filtered_time_errors.log"
start_time = "2024-03-19 14:00:00"
end_time = "2024-03-19 16:00:00"
with open(log_file, "r") as file, open(output_file, "w") as error_file:
for line in file:
if "ERROR" in line and start_time <= line[:19] <= end_time:
error_file.write(line)
Only errors from 14:00 to 16:00 will be saved in the “filtered_time.log” file.
Instantly Share Files with Python
If you need to share files with another computer, use Python’s built-in web server. Run this in the folder you want to share:
python3 -m http.server 8080
Now, anyone on your network can access your files at http://your-ip:8080.
Automate Python Scripts with Crontab
If you want a script to run automatically every day, hour, or even every 5 minutes, you can use crontab.
Edit your crontab:
crontab –e
Then, add this line to run script.py every 10 minutes:
*/10 * * * * /usr/bin/python3 /path/to/script.py
This line is a cron job entry that schedules script.py to run every 10 minutes.
Monitor System Resources
Check your CPU, RAM, and Disk Usage without opening another tool.
import psutil
def system_stats():
print("CPU Usage:", psutil.cpu_percent(interval=1), "%")
print("Memory Usage:", psutil.virtual_memory().percent, "%")
print("Disk Usage:", psutil.disk_usage('/').percent, "%")
if __name__ == "__main__":
system_stats()
This script will show real-time CPU, memory, and disk usage.
Conclusion
Python and Linux are a perfect pair for automation, helping you handle everything from file management to running multiple scripts.
With just a few lines of code, you can cut out repetitive tasks, save time, and make your workflow smoother than ever.