Linux Command - crontab 定期执行程序

Linux 系统定时任务则是由 cron (crond) 这个系统服务来控制的。用户可以使用crontab来创建自己的定时任务。

安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有则自动执行该任务。

使用 crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell 脚本。时间精确到分钟。一般用作日志分析和数据的定时备份。

命令参数:

usage: crontab [-u user] file
       crontab [-u user] { -e | -l | -r }
-l:列出定时任务
-e:编辑定时任务
-r:删除定时任务

第一次使用crontab -e 命令时会让我们选择编辑器,这时候可能会提示no crontab for root - using an empty one;这时候我们要重新选择编辑器,在终端输入:

export EDITOR=vim
举个例子

有一个文件main.sh

:cat main.sh
echo $(date) >> main

先输入 export EDITOR=vim

再输入crontab -e编辑定时任务,输入:

* * * * * /Users/lurongming/main.sh* * * * * /Users/lurongming/main.sh

表示没一分钟执行一次,一段时间后我们查看main文件的内容:

:cat main
Sun Mar 8 15:25:00 CST 2020
Sun Mar 8 15:26:00 CST 2020
Sun Mar 8 15:27:01 CST 2020
Sun Mar 8 15:30:00 CST 2020
Sun Mar 8 15:31:00 CST 2020
Sun Mar 8 15:32:00 CST 2020
Sun Mar 8 15:33:00 CST 2020
Sun Mar 8 15:34:00 CST 2020
Sun Mar 8 15:35:00 CST 2020
Sun Mar 8 15:36:00 CST 2020
Sun Mar 8 15:37:01 CST 2020

以上所有命令:

:cat main.sh
echo $(date) >> main
:export EDITOR=vim
:crontab -e
crontab: installing new crontab
:cat main
Sun Mar 8 15:25:00 CST 2020
Sun Mar 8 15:26:00 CST 2020
Sun Mar 8 15:27:01 CST 2020
Sun Mar 8 15:30:00 CST 2020
Sun Mar 8 15:31:00 CST 2020
Sun Mar 8 15:32:00 CST 2020
Sun Mar 8 15:33:00 CST 2020
Sun Mar 8 15:34:00 CST 2020
Sun Mar 8 15:35:00 CST 2020
Sun Mar 8 15:36:00 CST 2020
Sun Mar 8 15:37:01 CST 2020

Mac下可以参考:

     The first form of this command is used to install a new crontab from some named file or standard input if the
     pseudo-filename `-' is given.

     The following options are available:

     -u      Specify the name of the user whose crontab is to be tweaked.  If this option is not given, crontab exam-
             ines ``your'' crontab, i.e., the crontab of the person executing the command.  Note that su(1) can con-
             fuse crontab and that if you are running inside of su(1) you should always use the -u option for
             safety's sake.

     -l      Display the current crontab on standard output.

     -r      Remove the current crontab.

     -e      Edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.  The
             specified editor must edit the file in place; any editor that unlinks the file and recreates it cannot
             be used.  After you exit from the editor, the modified crontab will be installed automatically.

FILES
     /usr/lib/cron/cron.allow
     /usr/lib/cron/cron.deny

DIAGNOSTICS
     A fairly informative usage message appears if you run it with a bad command line.

SEE ALSO
     crontab(5), compat(5), cron(8), launchctl(1)

STANDARDS
     The crontab command conforms to IEEE Std 1003.2 (``POSIX.2'').  The new command syntax differs from previous
     versions of Vixie Cron, as well as from the classic SVR3 syntax.

AUTHORS
     Paul Vixie <[email protected]>

BSD                            December 29, 1993                           BSD

你可能感兴趣的:(Linux Command - crontab 定期执行程序)