标准输出和错误输出
- command 1 > fielname 把把标准输出重定向到一个文件中
- command > filename 2>&1 把把标准输出和标准错误一起重定向到一个文件中
常用命令
- ctrl-c: ( kill foreground process ) 发送 SIGINT 信号给前台进程组中的所有进程,强制终止程序的执行;
- ctrl-z: ( suspend foreground process ) 发送 SIGTSTP 信号给前台进程组中的所有进程,常用于挂起一个进程
- ctrl-d: 一个特殊的二进制值,表示 EOF,作用相当于在终端中输入exit后回车;
- ctrl-l:清屏幕
- clear:清除屏幕(与ctrl-l 效果不同)
- ctrl-s 中断控制台输出
- ctrl-q 恢复控制台输出
- jobs:列出后台运行序列号 (ctrl-z 挂起 bg)
- nohub:不挂断地运行命令
- &: 后台运行命令
- fg %num:将挂起的任务放在前台执行
- bg %num:将挂起的任务放在后台执行
1、crontab服务
查看crontab状态
sudo service cron status
开启crontab
sudo /etc/init.d/cron start
关闭crontab
sudo /etc/init.d/cron stop
重启crontab
sudo /etc/init.d/cron restart
2、crontab用法
crontab –e : 修改 crontab 文件,如果文件不存在会自动创建。
crontab –l : 显示 crontab 文件。
crontab -r : 删除 crontab 文件。
crontab -ir : 删除 crontab 文件前提醒用户。
3、格式
minute hour day-of-month month-of-year day-of-week commands
除了数字还有几个特殊的符号:"*"、"/"和"-"、","
- *代表所有的取值范围内的数字
- "/"代表每的意思,"/5"表示每5个单位
- "-"代表从某个数字到某个数字
- ","分开几个离散的数字
注:commands 注意以下几点
- 要是存在文件,要写绝对路径
- 即使是打印也不会显示在显示屏,在后台运行,最好重定向日志
4、编辑crontab文件
EDITOR=vi
export EDITOR
crontab -e
格式实例:
命令 | 说明 |
---|---|
*/1 * * * * service httpd restart 每一分钟 重启httpd服务 | 每一分钟 重启httpd服务 |
*/1 * * * * service httpd restart | 每一分钟 重启httpd服务 |
0 */1 * * * service httpd restart | 每一小时 重启httpd服务 |
30 21 * * * service httpd restart | 每天 21:30 分 重启httpd服务 |
26 4 1,5,23,28 * * service httpd restart | 每月的1号,5号 23 号 28 号 的4点26分,重启httpd服务 |
26 4 1-21 * * service httpd restart | 每月的1号到21号 的4点26分,重启httpd服务 |
*/2 * * * * service httpd restart | 每隔两分钟 执行,偶数分钟 重启httpd服务 |
1-59/2 * * * * service httpd restart | 每隔两分钟 执行,奇数 重启httpd服务 |
0 23-7/1 * * * service httpd restart | 每天的晚上11点到早上7点 每隔一个小时 重启httpd服务 |
0,30 18-23 * * * service httpd restart | 每天18点到23点 每隔30分钟 重启httpd服务 |
0-59/30 18-23 * * * service httpd restart | 每天18点到23点 每隔30分钟 重启httpd服务 |
59 1 1-7 4 * test ‘date +\%w’ -eq 0 && /root/a.sh | 四月的第一个星期日 01:59 分运行脚本 /root/a.sh ,命令中的 test是判断,%w是数字的星期几 |