linux write scheduled tasks

 

 Using crontab you can execute a shell script or a series of Linux commands at specified times.

For example, backup system logs every day

Recommended usage:

Execute in a linux shell window

crontab -e

An editing window will open, similar to vi

can use

 

Esc mode switch

:i enter insert mode, edit mode

:wq save and quit

:q! Quit without saving.

 

After entering the editing mode, you can enter and edit the specific timed task.

Scheduled task format

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  *  command to be executed

#Execute the shell script /data/backlog.sh with the root user at 0:00 am every day

0 0 * * *  /data/backlog.sh

 

Press Esc to exit the editing mode, then enter :wq to save and exit, the timed task is defined.

It is user-defined and will be written to the /var/spool/cron directory to generate a file with the same user name. The content of the file is the timing script we edited.

 Example in Red Hat system:

cd / var / spool / cron

vi root

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

AILTO=root

HOME=/

 

# For details see man 4 crontabs

 

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  *  command to be executed

 

23 * * * * /data/backlog/shell/backyeslog.sh

23 * * * * /data/backlog/shell/backtodaylog.sh

 

如果是在window下编辑完了上传到上面可能会有回车符的问题,解决办法请参看文章的末尾。

 

 

Crontab命令的选项

以下是 crontab 的有效选项:

crontab –e : 修改 crontab 文件. 如果文件不存在会自动创建。 

crontab –l : 显示 crontab 文件。 

crontab -r : 删除 crontab 文件。

crontab -ir : 删除 crontab 文件前提醒用户。

 

(系统级的)做系统级配置我们会直接配置 /etc/crontab

(用户级的)一般还是建议大家使用 crontab -e ,这样系统也会帮着检查我们配置的脚本语法。

系统级任务调度与用户级任务调度

系 统级任务调度主要完成系统的一些维护操作,用户级任务调度主要完成用户自定义的一些任务,可以将用户级任务调度放到系统级任务调度来完成(不建议这么 做),但是反过来却不行,root用户的任务调度操作可以通过“crontab –uroot –e”来设置,也可以将调度任务直接写入/etc /crontab文件,需要注意的是,如果要定义一个定时重启系统的任务,就必须将任务放到/etc/crontab文件,即使在root用户下创建一个 定时重启系统的任务也是无效的。

 

其他注意事项

新创建的cron job,不会马上执行,至少要过2分钟才执行。如果重启cron则马上执行。

 

当crontab突然失效时,可以尝试/etc/init.d/crond restart解决问题。或者查看日志看某个job有没有执行/报错tail -f /var/log/cron。

 

千万别乱运行crontab -r。它从Crontab目录(/var/spool/cron)中删除用户的Crontab文件。删除了该用户的所有crontab都没了。

 

在crontab中%是有特殊含义的,表示换行的意思。如果要用的话必须进行转义\%,如经常用的date ‘+%Y%m%d’在crontab里是不会执行的,应该换成date ‘+\%Y\%m\%d’。

 

定时任务crond服务的操作命令

 

情况一:正常情况(系统有service命令)

重启服务命令:[root@centos6 /]# service crond restart

 

启动服务命令:[root@centos6 /]# service crond start

 

停止服务命令:[root@centos6 /]# service crond stop

 

 

情况二:当linux发行的版本没有service这个命令时候,用如下命令进行停止启动:

 

停止服务:[root@centos6 /]# /etc/init.d/cron空格stop

 

启动服务:[root@centos6 /]# /etc/init.d/cron空格start

 

 

linux  系统中去掉^M的方法

  1. 对于回车符的定义: 
  2. windows:0D0A 
  3. unix\linux: 0A 
  4. MAC: 0D 
  5.  用VI的命令: 
  6.     使用vi打开文本文件 
  7.     vi dos.txt 
  8.     命令模式下输入 
  9.     :set fileformat=unix 
  10.     :wq 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326264687&siteId=291194637