How to automatically execute shell script at startup boot on systemd Linux

The following config will discuss a basic example on how to execute shell script during a boot time on systemd Linux. There maybe various reason why you might want to execute shell script during Linux startup like for example to start a particular custom service, check disk space, create a backup etc.

The following example below will serve as a basic template to be later modified to suit your specific needs. In the example below we will check a disk space of a /home/ directory during a boot time and write a report to /root/ directory.

Systemd service unit

First, we need to create a systemd startup script eg.disk-space-check.serviceand place it into /etc/systemd/system/ directory. You can find the example of such systemd startup script below:

[Unit]
After=mysql.service

[Service]
ExecStart=/usr/local/bin/disk-space-check.sh

[Install]
WantedBy=default.target

Read more

How to Use JSON API Data In Python

Objective

Consume a JSON API in Python.

Distributions

This will work on any Linux distribution.

Requirements

A working Linux install with Python.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

One of the main reasons that you’d like to work with JSON in Python is consuming APIs. There are hundreds of excellent public APIs out there and ready to use in your application. Even huge players on the web, like Facebook and Twitter, out out APIs for you to work with.

You can build entire applications around API data, including building web applications that aggregate, manipulate, and display that data in a convenient way.

Read more

How to Encode Data From Python To JSON

Objective

Encode Python data into JSON.

Distributions

This will work on any distribution with Python installed.

Requirements

A working Linux install with Python

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

JSON is the universal format for passing data between programs and/or languages, especially on the web. Python has excellent built-in support for converting data from a Python program into JSON for another program to use.

Read more

bash scripting

How to modify scripts behavior on signals using bash traps

Objective

The objective of this tutorial is to describe how to use the bash shell trap builtin to make our scripts able to perform certain actions when they receive a signal or in other specific situations.

Requirements

  • No special requirements

Difficulty

EASY

Conventions

  • # – requires given linux commands to be executed with root privileges either
    directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

bash scriptingWhen writing scripts that are meant to run for a considerable time, it’s very important to increase their robustness by making them able to react to system signals, executing specific actions when some of them are received. We can accomplish this task by using the bash trap builtin.

Read more

Python Exception Handling

Introduction

Python will let you know when you get your syntax wrong. It’ll immediately fail and not allow your program to run.

What about when your code has a different type of problem? Those are called exceptions, and they tend to be harder to catch. It’s up to you to recognize situations where hey might come up and catch them to prevent your program from crashing altogether.

Imagine a scenario where you need user input. Do you want your program to crash every time a user mistypes something or enters something erroneous? That’s far from ideal. Since you know there could be a problem there, you can tell Python to look out for one, and recover gracefully.

Read more

Python Packages and Modules

Introduction

Whenever you need some additional functionality in Python, you turn to the import keyword to pull in extras from Python modules. You’ve used common ones like the math module plenty of times.

Now, you will learn how to create your own Python modules and packages to compartmentalize your code. Modules are sort of like classes in that they make your code modular. While classes make code modular within a program and serve as the blueprints for objects, modules make all of your code modular across all programs and are utilities to be used just as they are.

Through the use of modules, you can create your own toolbox with all sorts of parts and pieces that you commonly use. Modules can include anything from variables and constants to functions and even classes. Because of this versatility, you can set yourself up to have everything that you need at the beginning of any project.

Read more

Python Inheritance

Introduction

Inheritance is yet another key concept in Object Oriented Programming, and it plays a vital role in building classes. It allows a class to be based off on an existing one.

When you first started writing Python classes, you were told to just put “Object” in the parenthesis of the class definition and not think too much about it. Well, now’s the time to start thinking about it.

“Object” is actually the base class that all Python classes inherit from. It defines a basic set of functionality that all Python classes should have. By inheriting from it when you create a new class, you ensure that that class has that basic functionality.

In short, inheritance is a nice way of categorizing classes and making sure that you don’t needlessly repeat yourself.

Read more

Experimenting With Numbers and Text In Python

Introduction

You probably want to jump in and start coding right away. That’s a great attitude to have, but it’s much better to experiment with the language and your programming environment first. If you’ve never programmed or never worked with an interpreted language like Python before, it’s important to get a feel for the way Python works and start to develop a workflow. One great aspect of Python being interpreted is the ability to write a couple of quick lines of code and test them out in real time. There really isn’t much setup beyond what you’ve already done.

Playing With Numbers

Without knowing anything about the language, you can use Python like a basic calculator. Open up either your .py file or the interpreter. Type in a basic math problem and run it.

>>> 10+25
35

Read more

Python Functions

Introduction

Code would quickly become an ugly and unruly mess if there wasn’t a way to easily repeat and reuse it. You’ve already seen some of that with loops. They’re great for repeating the same task multiple times right away. What if you wanted to reuse a block of code whenever you wanted? Well, that’s where functions come in.

Here’s another trip back to math class. If you remember, functions took in a number, did something to it, then outputted the new value. They were often represented in tables with the input on one side and the output on the other. Functions in programming are similar. Sometimes they take input. Sometimes they don’t. Much of the time they return a value as well, but they don’t always have to. In every case, they are used to repeat an operation whenever they are used, and that’s the greatest similarity with the math concept.

Read more

Python Variables

Introduction

Do you remember variables from math class in school? Variables in programming are actually very similar. Variables are just symbols that that represent a value and that value can be changed; thus the name variable. Unlike in math, variables in programming can be much more free form. Variables don’t just have to be a letter. Variables can be a single character, but they are more commonly a word or a short descriptive phrase in lower case with words separated by underscores. It’s actually best to name variables something descriptive so the you and anyone else that you’re working with knows exactly what that variable is, even much later on in the code.

Types of Variables

Python is a dynamic duck typed language. Don’t worry too much about the terminology, but that means that Python doesn’t force you to specify which types variables are when you create them. Oh yeah, there are types of variables. Even though you don’t necessarily have to specify their type when you create them, it’s a good idea to know what type you want them to be. Later on, having the wrong type of variable will invariably get you into big trouble.

Read more

Working With Number Variables In Python

Introduction

Obviously working with numbers in programming is important. Python as excellent mathematical capabilities, and there are tons of additional libraries available to extend Python’s built in functionality for even the most advanced calculations. Of course, the basics are important too, and numbers and some basic calculations come into play when controlling the flow of programs and making selections. That’s why knowing your way around working with numbers in Python is especially important.

Read more