Processes and Scheduling jobs using Cron in Linux

Hey All,

In today's blog, we are going to talk about how Linux can handle multiple tasks and resources all at a time.

In Linux, a process refers to a running instance of a program or command. Each process has its unique process ID (PID), which is used to identify and manage the process.

Processes can run in the foreground or background.

  • Foreground- A process that runs in the foreground requires user input and blocks other commands until it completes or is interrupted.

  • Background- A process that runs in the background runs independently of user input and allows other commands to be executed concurrently.

In addition to processes, Linux also supports the concept of jobs. A job is a command or set of commands that can be executed independently of the terminal session.

Jobs can be run in the foreground or background, and multiple jobs can be executed simultaneously.

NOTE: The main difference between a process and a job is that a job is a collection of processes that are related to a specific task, while a process is a single instance of a program that is running in memory.

Managing processes and jobs:

To see what processes and jobs are running currently, you can use various commands, such as:

  • ps: lists the currently running processes

  • jobs: lists the currently running jobs

  • fg: brings a job to the foreground

  • bg: runs a suspended job in the background

  • kill: terminates a process using its PID

There are also a few options that you can use with these commands.

So, Why does Linux has these Processes and Jobs?

  • Multitasking: Linux is a multi-user, multi-tasking operating system, which means that it can handle multiple tasks simultaneously. Processes and jobs provide a way to manage these tasks efficiently and ensure that they do not interfere with each other

  • Resource management: Processes and jobs allow for effective management of system resources such as memory, CPU, and disk space. This helps to ensure that the system runs smoothly and that resources are allocated appropriately.

  • Debugging: Processes and jobs provide a way to debug and troubleshoot the system when issues arise. By tracking the activities of specific processes and jobs, it is possible to identify the cause of problems and take appropriate action.

  • Automation: Processes and jobs are essential for automating tasks in Linux. By using tools like cron, it is possible to schedule jobs to run at specific times or intervals, allowing for hands-off management of the system.

In summary, processes and jobs are critical to the efficient and secure operation of Linux. They allow for multitasking, resource management, security, debugging, and automation, all of which are essential for managing complex systems.

Scheduling jobs using Cron:

Ever wondered how a task be performed at any defined time?

Well, it's by using Cron.

Cron is a time-based job scheduler in Linux that allows you to automate repetitive tasks such as backups, updates, and reports. With cron, you can schedule tasks to run at specific intervals or on specific days of the week, month, or year. Here, we'll explore how to use cron in Linux.

How we can use Cron?

Cron is a command-line tool that uses a configuration file called crontab to define the tasks that need to be executed. The crontab file contains a list of commands and their schedule of execution. To edit the crontab file, you can use the crontab command followed by the -e option:

$ crontab -e

This will open the crontab file in your default text editor. The crontab file has six fields separated by spaces or tabs:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday is both 0 and 7)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Each field specifies a particular aspect of the schedule for the command to be executed.

Commonly Used Cron Expressions:

Here are some commonly used expressions that can be used in crontab files:

  • * - every minute, hour, day of the month, month, or day of the week.

  • */n - every nth minute, hour, day of the month, or month.

  • n-m - every value between n and m, inclusive.

  • n,m - specific values n and m.

  • @reboot - run the command when the system reboots.

  • @yearly - run the command once a year at midnight on January 1st.

  • @monthly - run the command once a month at midnight on the first day of the month.

  • @weekly - run the command once a week at midnight on Sunday.

  • @daily - run the command once a day at midnight.

  • @hourly - run the command once an hour at the beginning of the hour.

For example, to execute the backup script at 2:00 am every day, we can use the following expression:

0 2 * * * /path/backup/script.sh

To run a command every 5 minutes, we can use the following expression:

*/5 * * * * command

To run a command every hour on weekdays, we can use the following expression:

0 * * * 1-5 command

This will execute the command every hour from Monday to Friday.

Hence we can say that Cron can help automate repetitive tasks and improve system efficiency.

That's it for today, hope to see you in the next one soon :)