Linux crontab 是用来定期执行程序的命令。
当安装完成操作系统之后,默认便会启动此任务调度命令。
crond 命令每分钟会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。
注意:新创建的 cron 任务,不会马上执行,至少要过 2 分钟后才可以,当然你可以重启 cron 来马上执行。
而 linux 任务调度的工作主要分为以下两类:
crontab [ -u user ] file
或
crontab [ -u user ] { -l | -r | -e }
说明:
crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表。
-u user 是指设定指定 user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设定自己的时程表。
参数说明:
时间格式如下:
f1 f2 f3 f4 f5 program
* * * * * - - - - - | | | | | | | | | +----- 星期中星期几 (0 - 6) (星期天 为0) | | | +---------- 月份 (1 - 12) | | +--------------- 一个月中的第几天 (1 - 31) | +-------------------- 小时 (0 - 23) +------------------------- 分钟 (0 - 59)
使用者也可以将所有的设定先存放在文件中,用 crontab file 的方式来设定执行时间。
每一分钟执行一次 /bin/ls:
* * * * * /bin/ls
在 12 月内, 每天的早上 6 点到 12 点,每隔 3 个小时 0 分钟执行一次 /usr/bin/backup:
0 6-12/3 * 12 * /usr/bin/backup
周一到周五每天下午 5:00 寄一封信给 alex@domain.name:
0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata
每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha":
20 0-23/2 * * * echo "haha"
下面再看看几个具体的例子:
0 */2 * * * /sbin/service httpd restart 意思是每两个小时重启一次apache 50 7 * * * /sbin/service sshd start 意思是每天7:50开启ssh服务 50 22 * * * /sbin/service sshd stop 意思是每天22:50关闭ssh服务 0 0 1,15 * * fsck /home 每月1号和15号检查/home 磁盘 1 * * * * /home/bruce/backup 每小时的第一分执行 /home/bruce/backup这个文件 00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; 每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。 30 6 */10 * * ls 意思是每月的1、11、21、31日是的6:30执行一次ls命令
注意:当程序在你所指定的时间执行后,系统会发一封邮件给当前的用户,显示该程序执行的内容,若是你不希望收到这样的邮件,请在每一行空一格之后加上 > /dev/null 2>&1 即可,如:
20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh > /dev/null 2>&1
如果我们使用 crontab 来定时执行脚本,无法执行,但是如果直接通过命令(如:./test.sh)又可以正常执行,这主要是因为无法读取环境变量的原因。
解决方法:
1、所有命令需要写成绝对路径形式,如: /usr/local/bin/docker。
2、在 shell 脚本开头使用以下代码:
#!/bin/sh . /etc/profile . ~/.bash_profile
3、在 /etc/crontab 中添加环境变量,在可执行命令之前添加命令 . /etc/profile;/bin/sh,使得环境变量生效,例如:
20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh
Linux clock命令用于调整 RTC 时间。
RTC 是电脑内建的硬件时间,执行这项指令可以显示现在时刻,调整硬件时钟的时间,将系统时间设成与硬件时钟之时间一致,或是把系统时间回存到硬件时钟。
clock [--adjust][--debug][--directisa][--getepoch][--hctosys][--set --date="<日期时间>"][--setepoch --epoch=< >][--show][--systohc][--test][--utc][--version]
参数说明:
获取当前的时间
# clock //获取当前的时间
显示UTC时间
# clock -utc //显示UTC时间
Linux chroot (英文全称:change root) 命令用于改变根目录。
chroot 命令把根目录换成指定的目的目录。
chroot [--help][--version][目的目录][执行指令...]
参数说明:
改变根目录
# chroot /mnt/ls //改变根目录
Linux bind命令用于显示或设置键盘按键与其相关的功能。
您可以利用bind命令了解有哪些按键组合与其功能,也可以自行指定要用哪些按键组合。
bind [-dlv][-f <按键配置文件>][-m <按键配置>][-q <功能>]
参数说明:
显示按键组合的所有功能
# bind -l //显示按键组合的内容 abort accept-line alias-expand-line arrow-key-prefix backward-byte backward-char backward-delete-char backward-kill-line backward-kill-word backward-word beginning-of-history beginning-of-line ……省略部分内容 vi-goto-mark vi-insert-beg vi-insertion-mode vi-match vi-movement-mode vi-next-word vi-overstrike vi-overstrike-delete vi-prev-word vi-put vi-redo vi-replace vi-rubout vi-search vi-search-again vi-set-mark vi-subst vi-tilde-expand vi-yank-arg vi-yank-to yank yank-last-arg yank-nth-arg yank-pop
显示当前按键组合的设置
# bind -l abort accept-line alias-expand-line arrow-key-prefix backward-byte backward-char backward-delete-char backward-kill-line backward-kill-word backward-word beginning-of-history beginning-of-line call-last-kbd-macro capitalize-word character-search character-search-backward clear-screen complete complete-command complete-filename complete-hostname complete-into-braces complete-username complete-variable copy-backward-word copy-forward-word copy-region-as-kill dabbrev-expand delete-char delete-char-or-list delete-horizontal-space digit-argument display-shell-version do-lowercase-version downcase-word dump-functions dump-macros dump-variables dynamic-complete-history edit-and-execute-command emacs-editing-mode end-kbd-macro end-of-history end-of-line exchange-point-and-mark forward-backward-delete-char forward-byte forward-char forward-search-history forward-word glob-complete-word glob-expand-word glob-list-expansions history-and-alias-expand-line history-expand-line history-search-backward history-search-forward insert-comment insert-completions insert-last-argument kill-line kill-region kill-whole-line kill-word magic-space menu-complete menu-complete-backward next-history non-incremental-forward-search-history non-incremental-forward-search-history-again non-incremental-reverse-search-history non-incremental-reverse-search-history-again old-menu-complete operate-and-get-next overwrite-mode possible-command-completions possible-completions possible-filename-completions possible-hostname-completions possible-username-completions possible-variable-completions previous-history quoted-insert redraw-current-line re-read-init-file reverse-search-history revert-line self-insert set-mark shell-backward-kill-word shell-backward-word shell-expand-line shell-forward-word shell-kill-word skip-csi-sequence start-kbd-macro tab-insert tilde-expand transpose-chars transpose-words tty-status undo universal-argument unix-filename-rubout unix-line-discard unix-word-rubout upcase-word vi-append-eol vi-append-mode vi-arg-digit vi-back-to-indent vi-bword vi-bWord vi-change-case vi-change-char vi-change-to vi-char-search vi-column vi-complete vi-delete vi-delete-to vi-editing-mode vi-end-word vi-eof-maybe vi-eword vi-eWord vi-fetch-history vi-first-print vi-fword vi-fWord vi-goto-mark vi-insert-beg vi-insertion-mode vi-match vi-movement-mode vi-next-word vi-overstrike vi-overstrike-delete vi-prev-word vi-put vi-redo vi-replace vi-rubout vi-search vi-search-again vi-set-mark vi-subst vi-tilde-expand vi-yank-arg vi-yank-to yank yank-last-arg yank-nth-arg yank-pop root@snail-hnlinux:~# root@snail-hnlinux:~# root@snail-hnlinux:~# root@snail-hnlinux:~# root@snail-hnlinux:~# bind -v set bind-tty-special-chars on set blink-matching-paren on set byte-oriented off set completion-ignore-case off set convert-meta off set disable-completion off set echo-control-characters on set enable-keypad off set enable-meta-key on set expand-tilde off set history-preserve-point off set horizontal-scroll-mode off set input-meta on set mark-directories on set mark-modified-lines off set mark-symlinked-directories off set match-hidden-files on set meta-flag on set output-meta on set page-completions on set prefer-visible-bell on set print-completions-horizontally off set revert-all-at-newline off set show-all-if-ambiguous off set show-all-if-unmodified off set skip-completed-text off set visible-stats off set bell-style audible set comment-begin # set completion-prefix-display-length 0 set completion-query-items 100 set editing-mode emacs set history-size 1000 set keymap emacs
列出指定功能的按键和按键组合
# bind -q abort //请用 调用abort “C-g”, “C-xC-g”, “eC-g”. # bind -q accept-line //列出功能“accept-line”按键以及组合按键 //请用 调用accept-line “C-j”, “C-m”.
Linux aumix命令用于设置音效装置。
aumix(audio mixer)命令设置各项音效装置的信号强度以及指定播放与录音的装置。
aumix [-123bcilmoprstvwWx][(+/-)强度][PqR][-dfhILqS]
参数说明:[-123bcilmoprstvwWx]为频道参数,用来指定装置的频道;[PqR]可用来指定播放或录音装置;[-dfhILqS] 则为指令参数。若不加任何参数,aumix会显示简单的图形界面供调整设置频道参数。
指令参数:
设置音效设备
# aumix
Linux dircolors命令用于设置 ls 指令在显示目录或文件时所用的色彩。
dircolors可根据[色彩配置文件]来设置LS_COLORS环境变量或是显示设置LS_COLORS环境变量的shell指令。
dircolors [色彩配置文件]
或
dircolors [-bcp][--help][--version]
参数说明:
显示默认值
# dircolors -p //显示默认值 # Configuration file for dircolors, a utility to help you set the # LS_COLORS environment variable used by GNU ls with the --color option. # Copyright (C) 1996, 1999-2008 # Free Software Foundation, Inc. # Copying and distribution of this file, with or without modification, # are permitted provided the copyright notice and this notice are preserved. # The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the # slackware version of dircolors) are recognized but ignored. # Below, there should be one TERM entry for each termtype that is colorizable TERM Eterm TERM ansi TERM color-xterm TERM con132x25 TERM con132x30 TERM con132x43 TERM con132x60 TERM con80x25 TERM con80x28 TERM xterm-debian # Below are the color init strings for the basic file types. A color init # string consists of one or more of the following numeric codes: # Attribute codes: # 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed # Text color codes: # 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white # Background color codes: # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white #NORMAL 00 # no color code at all #FILE 00 # regular file: use no color at all RESET 0 # reset to “normal“ color DIR 01;34 # directory LINK 01;36 # symbolic link. (If you set this to 'target' instead of a # numerical value, the color is as for the file pointed to.) HARDLINK 44;37 # regular file with more than one link FIFO 40;33 # pipe SOCK 01;35 # socket DOOR 01;35 # door BLK 40;33;01 # block device driver CHR 40;33;01 # character device driver ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file SETUID 37;41 # file that is setuid (u+s) SETGID 30;43 # file that is setgid (g+s) CAPABILITY 30;41 # file with capability STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w) OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable # This is for files with execute permission: EXEC 01;32 # List any file extensions like '.gz' or '.tar' that you would like ls # to colorize below. Put the extension, a space, and the color init string. # (and any comments you want to add after a '#') # If you use DOS-style suffixes, you may want to uncomment the following: #.cmd 01;32 # executables (bright green) #.exe 01;32 #.com 01;32 #.btm 01;32 #.bat 01;32 # Or if you want to colorize scripts even if they do not have the # executable bit actually set. #.sh 01;32 #.csh 01;32 # archives or compressed (bright red) .tar 01;31 .pcx 01;35 .mov 01;35 .mpg 01;35 .mpeg 01;35 .m2v 01;35 .mkv 01;35 .ogm 01;35 .mp4 01;35 .m4v 01;35 .mp4v 01;35 .vob 01;35 .qt 01;35 .nuv 01;35 .wmv 01;35 .asf 01;35 .rm 01;35 .rmvb 01;35 .flc 01;35 .avi 01;35 .fli 01;35 .flv 01;35 .gl 01;35 .dl 01;35 .xcf 01;35 .xwd 01;35 .yuv 01;35 # http://wiki.xiph.org/index.php/MIME_Types_and_File_Extensions .axv 01;35 .anx 01;35 .ogv 01;35 .ogx 01;35 # audio formats .aac 00;36 .au 00;36 .flac 00;36 .mid 00;36 .midi 00;36 .mka 00;36 .mp3 00;36 .mpc 00;36 .ogg 00;36 .ra 00;36 .wav 00;36 # http://wiki.xiph.org/index.php/MIME_Types_and_File_Extensions .axa 00;36 .oga 00;36 .spx 00;36 .xspf 00;36
Linux alias命令用于设置指令的别名。
用户可利用alias,自定指令的别名。若仅输入alias,则可列出目前所有的别名设置。alias的效力仅及于该次登入的操作。若要每次登入是即自动设好别名,可在.profile或.cshrc中设定指令的别名。
alias[别名]=[指令名称]
参数说明:若不加任何参数,则列出目前所有的别名设置。
给命令设置别名
# alias lx=ls # lx anaconda-ks.cfg Desktop install.log install.log.syslog qte
Linux reset命令其实和 tset 是一同个命令,它的用途是设定终端机的状态。一般而言,这个命令会自动的从环境变数、命令列或是其它的组态档决定目前终端机的型态。如果指定型态是 '?' 的话,这个程序会要求使用者输入终端机的型别。
由于这个程序会将终端机设回原始的状态,除了在 login 时使用外,当系统终端机因为程序不正常执行而进入一些奇怪的状态时,你也可以用它来重设终端机o 例如不小心把二进位档用 cat 指令进到终端机,常会有终端机不再回应键盘输入,或是回应一些奇怪字元的问题。此时就可以用 reset 将终端机回复至原始状态。
tset [-IQqrs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]
参数说明:
让使用者输入一个终端机型别并将终端机设到该型别的预设状态
# reset ?
将 erase 字元设定 control-h
# reset -e ^B
将设定用的字串显示在屏幕上
# reset -s Erase is control-B (^B). Kill is control-U (^U). Interrupt is control-C (^C). TERM=xterm;
let 命令是 BASH 中用于计算的工具,用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量。如果表达式中包含了空格或其他特殊字符,则必须引起来。
let arg [arg ...]
arg:要执行的表达式
自加操作:let no++
自减操作:let no--
简写形式 let no+=10,let no-=20,分别等同于 let no=no+10,let no=no-20。
以下实例计算 a 和 b 两个表达式,并输出结果:
#!/bin/bash let a=5+4 let b=9-3 echo $a $b
以上实例执行结果为:
9 6