Advertisement

Advertisement

Cron Job Examples and Tip

Cron jobs are useful for scheduling commands to run periodically. For example:

  • Every 15 minutes
  • Once per month

In this guide I will show you several common cron tasks and tips that I use including:

  • Common scheduling examples
  • Chaining together multiple programs
  • Redirecting output
  • Checking logs
  • Running a script from a Python virtual environment

Cron is good for programs that run and then complete, like generating a report, but cron is not for services that stay running forever like a web server. If you want to have a program that continuously runs and you want to ensure the program stays running even if it crashes or the server restarts, you want to setup a service. Check out my tutorial on how to do that: Creating Systemd Service Files.

Cron is available in Linux and Mac operating systems, but not Windows.

How to Specify SSH key for Git repository

If you are using SSH keys with Git to clone and pull your repositories, you may have to manage several SSH keys. For example, it is common to setup a "deploy key" in GitHub (Repository | Settings | Deploy Keys) that has read-only rights. GitHub also forces you to use unique SSH deploy keys for each repository, so you have to create a unique SSH keys when you have multiple repositories.

This example shows you how to use specific SSH keys for each remote repository.

How to Create a Secure Linux System User

When deploying a production service in Linux you want to configure it as securely as possible. Ideally, you will create a unique Linux user for each service and give them only read and write permission to the exact files they need.

You can go even further and create a "system" user that has no home directory, no login shell, and no password. This prevents the user from being able to login and does not provide a home directory for them to store files.

If the service was ever compromised this limits the actions an attacker can take with the user running the service.

This example will show you how to create a system user with:

  • No home directory
  • No logn shell
  • No password (Can't login)

Open a Linux Firewall port with firewall-cmd

In Fedora/CentOS/RedHat, the firewall is on by default. This is a good secure-by-default practice. If you do not know that the firewall is on though, you may be wondering why you cannot connect to a web service that is listening on your machine and works fine locally, but external connections cannot be made.

This example will demonstrate how to open inbound ports and also check what ports, service, and zones are available on your machine.

Run Python WSGI Web App with Waitress

WSGI is the standard for running Python web applications. Gunicorn and uWSGI are great options for running WSGI apps, but they only have limited support in Windows or no support at all. Waitress is another option for running WSGI applications and it really shines in Windows because it is written purely in Python with no other dependencies. I will show you how to use Waitress to serve web application using Python WSGI.

Run Python Script as Windows Service

If you want to run a Python script as a Windows service you can use the Non-Sucking Service Manager to create the service. This application allows you to easily create a service using any executable or batch script that gets treated as a proper Windows service that you can manage Windows services by running services.msc or by going to the Services tab from the Task Manager.

The most common use case for running a Python script as a service is to ensure an application is always run and starts running when you log in. For example, if you have a web application that you want to stay running whenever the computer is up. I have used this method to run Flask applications and other utilities.

Working with Dates and Times in Python 3

Working with dates and times in any programming language can be a huge hassle. There are so many ways to mess up when using different date and time formats, dealing with timezones, and accounting for daylight savings time. There are a lot of things to remember to get right. I'll show you everything you need to know about working with dates and times in Python to make it easy so you can feel confident any time you have work with them.

How to Setup Drupal for Local Development with Just PHP

With older versions of Drupal you could not run Drupal without Apache or Nginx to perform the URL rewrites needed to use the application with clean URLs.

New versions of Drupal (8+) come with a .ht.router.php file that is specifically used to run local development instances with just the built-in PHP web server. No Apache, no Wamp, no Mamp, no Xampp. Just the built-in PHP server.

In addition, you don't have to use MySQL an setup a separate database. Drupal supports SQLite3 so can completely skip any MySQL setup.

This means you can run a local Drupal development environment with nothing but PHP.

  • No Apache
  • No MySQL
  • No Wamp,Xampp,Mamp
  • Just PHP

Following the method in this guide works on Windows, Mac, and Linux equally.

Run JavaScript After DOM is Fully Loaded

Introduction

When writing JavaScript for a web browser, there are times when you want the page to fully load before the code is executed. This is particularly true if your JavaScript manipulates or utilized the HTML DOM. Your JavaScript will fail if it tries to reference an element that has not yet been loaded. This example will show you how to defer code from being executed until the web page has fully loaded.

Tips and Tricks for Colab

Colaboratory, or Colab for short, is a browser-based Python interpreter built by Google. It extends the open source Jupyter Notebook project.

With additional niceties such as Markdown previews, free GPU, and the ability to make a comment just like you would any Google Docs, Colab is quickly becoming the Python environment of choice among developers in machine learning, data science, and AI research spaces.

Here are few practical Tips and Tricks to help you get the most out of Colab.

Working with Git Repositories in Python

The GitPython project allows you to work in Python with Git repositories. In this guide we'll look at some basic operations like:

  • Initializing a repo
  • Cloning a repo
  • Adding and committing
  • Pushing and pulling with remotes
  • Checking for changes
  • Getting a diff
  • Listing and switching branches

For full documentation, see https://gitpython.readthedocs.io/. The source code is available at https://github.com/gitpython-developers/GitPython.

Advertisement

Advertisement