Sobes.tech
Junior

What programming languages do you use for automating routine tasks in your work?

sobes.tech AI

Answer from AI

Python. Flexibility, rich libraries (os, subprocess, requests, paramiko), and a large community make it an excellent choice for automation scripts related to deployment, monitoring, and infrastructure management.

Bash. Necessary for writing shell scripts, automating command-line tasks, working with files and directories, and integrating various tools into CI/CD pipelines.

Go. Used for writing more performant and complex automation tools, microservices, and utilities where execution speed and parallelism are important.

Sometimes I use Ruby or Perl for working with legacy systems or scripts written earlier.

Example of automation in Python:

import subprocess
import os

def deploy_app(server_ip, app_path):
  """
  Deploys an application to a remote server.
  """
  try:
    # Example commands for scp and ssh execution
    subprocess.run(["scp", app_path, f"user@{server_ip}:/opt/app/"], check=True)
    subprocess.run(["ssh", f"user@{server_ip}", "sudo systemctl restart myapp"], check=True)
    print(f"Application {app_path} successfully deployed to {server_ip}")
  except subprocess.CalledProcessError as e:
    print(f"Deployment error: {e}")

# Example function call
# deploy_app("192.168.1.10", "/path/to/my_app.tar.gz")

Example of automation in Bash:

#!/bin/bash

# Automatic cleanup of old logs
LOG_DIR="/var/log/myapp"
FIND_CMD="find ${LOG_DIR} -type f -mtime +7 -name '*.log'"

# Delete logs older than 7 days
eval "${FIND_CMD} -delete" || { echo "Error deleting logs"; exit 1; }

echo "Old logs cleaned up."