NanoDano's blog

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.

Advertisement

Advertisement