Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report