Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use cron to customize tasks by time in Linux system

Shulou Source: shulou.com Published: 2022-06-01 22:19:19 10月04日 Update

How to use cron to customize tasks by time in Linux system? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

Cron is the default scheduled task in Linux. With cron, you can schedule a schedule (such as commands or shell scripts) to run periodically or at specific times such as minutes, hours, days, weeks, months, etc. Cron is useful when you schedule different routine maintenance tasks, such as periodic backups, log loops, checking file systems, monitoring disk space, and so on.

Add a cron task from the command line

To add cron tasks, you can use a command-line tool called crontab.

Entering the following command creates a new cron task that runs as the current user.

The code is as follows:

$crontab-e

If you want to run the cron task as another user, enter the following command.

The code is as follows:

$sudo crontab-u-e

You will see a text editing window where you can add or edit cron tasks. The nono editor is used by default.

The format of each cron task is as follows.

The code is as follows:

The first five elements define the plan for the task, and the last element is the full path to the command or script.

Here are some examples of cron tasks.

* / home/dan/bin/script.sh: run every minute.

0 * / home/dan/bin/script.sh: run every hour.

0 * / home/dan/bin/script.sh: runs at 0 o'clock every day.

09 9AM 18 * / home/dan/bin/script.sh: run on 9AM and 6PM every day.

09-18 * / home/dan/bin/script.sh: runs every hour from 9AM to 6PM.

0 9-18 * 1-5 / home/dan/bin/script.sh: 9AM to 6PM runs hourly from Monday to Friday.

* / 10 * / home/dan/bin/script.sh: run every 10 minutes.

Once you have completed the above setup steps, press Ctrl+X to save and exit the editor. At this point, the new scheduled task should have been activated.

To view the scheduled tasks that exist, use the following command:

The code is as follows:

$crontab-l

Add scheduled tasks from GUI

If you are in a Linux desktop environment, you can use crontab's more friendly GUI front end to add or add a cron task.

On the Gnome desktop, there is a Gnome Schedule (gnome-schedule package).

On the KDE desktop, there is a Task Scheduler (kcron package).

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Tasks runs commands code hours facets desktops times systems elements cycles periodicity users editors scripts help input difference clarity creativity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info MariaDB macOS Redmi