Author: Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

We can use the trap command to catch the error signal system by the system during script execution. Then you can execute a shell command or call a function. In this way, you can execute your custom script code on an error that occurred in a bash script. This can be helpful to revert any partial changes, close database connections, or email status to the concerned persons, etc. You can use trap commands with `ERR` signals like: trap ‘on_error_function’ ERR When an error is generated in a shell script, it will execute a function named ‘on_error_function’ of your shell script.…

Read More

Question – How do I list all the collections available in the MongoDB database? MongoDB is a NoSQL database, that stores documents in JSON format. A collection is an entity in MongoDB (ie similar to a table in RDBMS databases) that stores JSON documents. You can use one of the below options to list collections in a MongoDB database. Mongo Shell: You can use one of the following commands from Mongo Shell to list all available collections in the MongoDB database. Before running the below commands you must be connected with the target database using use database_name. show collections show…

Read More

MongoDB find() method is used to select documents from a specified collection. It also set the cursor position to the selected document. The default find() method gets the documents from the start of the collection. In some situations, we need to fetch the last record from a collection. Use the following query to get the last records from a MongoDB collection. db.collection.find().limit(1).sort({$natural:-1}) The above query provides the single last record only, You can also fetch the multiple records from the end of the MongoDB collection. You can set the number of records with limit() method as below: db.collection.find().limit(5).sort({$natural:-1}) Thanks, Hope…

Read More

Cron is a service that runs tasks at specified intervals in Unix/Linux systems. It’s commonly used for operational tasks like cleaning log files or backing up databases. But for our purposes, we can also use it to automate applications to perform some tasks at regular intervals. In this article, we will see how we can run a job cron job every 10 minutes, 20 minutes, or 30 minutes. Use the following timer options to schedule a job in crontab to run on specified intervals. Running a corn every 10 minutes

Running a corn every 20 minutes

Running a…

Read More

Running a job every 5 minutes is a commonly used cron schedule. In this quick how-to article, you will learn to schedule a cronjob to run every 5 minutes. Syntax: Use the following syntax to schedule a command or script to run every 5 minutes using crontab.

Example To schedule a PHP script to run every 5 minutes. Run the command crontab -e to edit crontab in an editor and schedule your job. You can use crontab -e command to edit the crontab editor:

The cronjob will run the above command at 5-minute intervals.

Read More

Sometimes the shell scripts are required to run as the root user to perform some tasks with administrator privileges. Generally, that tasks can be achieved with the Sudo commands. If in some cases you still need to force the user to run the script as the root user, you can add code to your script to check if the script is running with the root user or not. Check if a shell script running as root user Add the following code at beginning of the shell script to check if the script is running as the root user. If the…

Read More

The use of the YYYY-MM-DD date format in shell scripting is highly beneficial for ensuring consistency, clarity, and sorting efficiency in scripts that handle dates. This article will explore the advantages of this format and provide practical examples of its implementation in shell scripts. Introduction In shell scripting, managing dates effectively is crucial for tasks such as logging, scheduling, and data manipulation. The ISO 8601 standard, which specifies YYYY-MM-DD as the date format, is widely recognized for its clarity and unambiguity. This format is particularly useful in shell scripts due to its straightforwardness and universality. Benefits of YYYY-MM-DD Format Sorting…

Read More

Boolean variables are an essential part of programming, as they represent true or false values and are often used to control the flow of a script. In shell scripting, boolean variables are used to make decisions, control loops, and manage conditional execution. This article provides a comprehensive guide to declaring and using boolean variables in different shells, such as Bash, Zsh, and Ksh. We will also explore common use cases for boolean variables in shell scripts, share tips and best practices for working with boolean variables, and provide examples of boolean variable usage in real-world shell scripts. Introduction In shell…

Read More
GIT

Git is a powerful version control system that is widely used by software developers. One of the key features of Git is the ability to create and manage multiple branches within a single repository. In this article, we will take a step-by-step approach to explain the process of creating Git branches. Step 1: Understanding Git Branches Before we dive into creating Git branches, it is important to understand what branches are and why they are used. A Git branch is essentially a separate line of development within a Git repository. It allows you to work on different parts of your…

Read More

Cron is an incredibly useful tool in the Linux operating system. It allows users to schedule tasks, also known as jobs, to run automatically at specific times or dates. While it is designed to schedule tasks to run at least once per minute, there are techniques you can use to run tasks every 30 seconds. This article provides a deep dive into understanding how to manipulate cron jobs in such a way to run tasks on a more frequent schedule. What is a Cron Job? The term “cron” comes from the Greek word “Chronos”, which means time. In Linux, cron…

Read More