Basic operation linux (centOS7) (4) Timing task --crontab

Outline

For Java developers, is no stranger to regular tasks, it is nothing more than allow the system to perform a specific command or program at a specific time. E.g. spring provided @Scheduled annotations, Quartz frame OpenSymphony provided, scheduled tasks can be implemented. In this level operating system, linux also provides a corresponding solution --crontab command.

 

Crontab command format

crontab [-e] [-l] [-r]

The latter three options must choose one, and can only choose one.

-e: edit regular tasks;

-l: Displays the current timing all tasks in a list;

-r: delete all current regular tasks.

Wherein, crontab -e vi editor will pop up, scheduled tasks can be written in two ways:

1. cron expression command +

2. cron expression + shell script file

Here's cron expression with https://www.cnblogs.com/dubhlinn/p/10740838.html slightly different article mentioned, the smallest in minutes, and without a question mark (?), Only manually to avoid date and week conflict.

 

Direct editing commands way to build regular tasks

Steps for usage

1. Perform crontab -e

2. In the editor writes vi: cron expressions, commands, intermediate spaces interval

3. Save and exit

Examples

For example, at midnight 23 points per day under the current directory is appended to the file /home/dubhlinn/content.log

0 23 * * * ls -l >> /home/dubhlinn/content.log

 

Save command shell script way to build regular tasks

Steps for usage

1. Create a shell script file (.sh), in which the write command

2. give "execute" permission for the shell script file

3. Perform crontab -e

4. In the dialog editor writes: Absolute path cron expression, shell script file, a space center spacing

5. Save and exit

Examples

Every 15 minutes, for example, the current date and time is added to the file /home/dubhlinn/time.log.

First, create a home directory / home / dubhlinn under timer.sh file, which is written: date >> /home/dubhlinn/time.log.

Then, the shell script file you just created, execute permissions given to the owner: chmod u + x timer.sh.

Finally, execute crontab -e, and writes in the editor: * / 15 * * * * /home/dubhlinn/timer.sh.

 

   

Guess you like

Origin www.cnblogs.com/dubhlinn/p/11094051.html