标签:硬件 进程间 -name rand path 级别 cat 进程间通信 自定义服务
任务计划crontab计划任务文件
任务计划文件路径/var/spool/cron/
cat /etc/crontab
[root@shu-test ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
# | | | | |
# * * * * * user-name command to be executed
[root@shu-test ~]#
格式:分钟 小时 日 月 周 user command
范围:
分钟:0-59;
小时:0-23;
日:0-31;
月:0-12;
周:0-6,0为周日、或7为周日;可用格式:
*:表示所有;
1-5:表示范围为1到5;
1,2,3:表示1 2 3;*
/2:表示被2整除的数字,比如小时,那就是没隔2小时;**
举例:
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
0分 3点 1到10日 偶数月份 星期2或星期5 使用/bin/bash的内核shell 执行脚本123.sh无论正确错误都将输入123.log日志;
当偶数月份的1到10日是星期2或星期5的时候,那么就在凌晨3:00执行后面这个脚本;
crontab -e
crontab -l
[root@shu-test ~]# crontab -l
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
[root@shu-test ~]#
cat /var/spool/cron/root
[root@shu-test ~]# cat /var/spool/cron/root
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@shu-test ~]#
systemctl start crond
ps aux |grep crond
[root@shu-test ~]# ps aux |grep crond
root 543 0.0 0.1 126268 1664 ? Ss 14:47 0:00 /usr/sbin/crond -n
root 1372 0.0 0.0 112676 980 pts/0 R+ 16:34 0:00 grep --color=auto crond
[root@shu-test ~]#
或者使用命令
systemctl status crond
显示结果: Active: active (running)
[root@shu-test ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2018-01-29 14:47:53 CST; 1h 47min ago
Main PID: 543 (crond)
CGroup: /system.slice/crond.service
└─543 /usr/sbin/crond -n
1月 29 14:47:53 shu-test systemd[1]: Started Command Scheduler.
1月 29 14:47:53 shu-test systemd[1]: Starting Command Scheduler...
1月 29 14:47:53 shu-test crond[543]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 29% if used.)
1月 29 14:47:53 shu-test crond[543]: (CRON) INFO (running with inotify support)
1月 29 16:33:01 shu-test crond[543]: (root) RELOAD (/var/spool/cron/root)
[root@shu-test ~]#
CentOS 6上的服务管理工具为chkconfig,Linux系统所有的预设置服务都可以通过查看/etc/init.d目录得到;
[root@shu-test ~]# ls /etc/init.d
functions netconsole network README
[root@shu-test ~]#
chkconfig --list
[root@shu-test ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]‘。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@shu-test ~]#
级别(0~6):为系统启动级别,0、1、6被系统保留。可以参照init命令;
0:为shutdown动作;
1:作为重启到单用户模式;
6:为重启;
2:无NFS支持的多用户模式;
3:完全多用户模式;
4:自定义级别;
5:图形界面;
chkconfig --level 3 network off
关闭network的3级别
[root@shu-test ~]# chkconfig --level 3 network off
[root@shu-test ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]‘。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:关 4:开 5:开 6:关
[root@shu-test ~]#
chkconfig --level 3 network on
开启network服务的3级别
[root@shu-test ~]# chkconfig --level 3 network on
[root@shu-test ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]‘。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@shu-test ~]#
chkconfig --add 123
将定义好的服务123添加到启动服务中,使用vim就行编辑123文件;
[root@shu-test ~]# cd /etc/init.d/
[root@shu-test init.d]# ls
functions netconsole network README
[root@shu-test init.d]# cp network 123
[root@shu-test init.d]# ls
123 functions netconsole network README
[root@shu-test init.d]#
[root@shu-test init.d]# chkconfig --add 123
[root@shu-test init.d]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]‘。
123 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@shu-test init.d]#
chkconfig --del 123
删除123服务
[root@shu-test init.d]# chkconfig --del 123
[root@shu-test init.d]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]‘。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@shu-test init.d]#
用于CentOS 7系统下、CentOS 6系统常用chkconfig工具;
systemctl list-units --all --type=service
systemctl enable crond.service
systemctl disable crond
systemctl status crond
[root@shu-test init.d]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2018-01-29 16:37:09 CST; 3h 54min ago
Main PID: 1427 (crond)
CGroup: /system.slice/crond.service
└─1427 /usr/sbin/crond -n
1月 29 16:37:09 shu-test systemd[1]: Started Command Scheduler.
1月 29 16:37:09 shu-test systemd[1]: Starting Command Scheduler...
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 8% if used.)
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (running with inotify support)
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (@reboot jobs will be run at computer‘s startup.)
1月 29 16:47:01 shu-test crond[1427]: (root) RELOAD (/var/spool/cron/root)
[root@shu-test init.d]#
systemctl stop crond
systemctl start crond
systemctl restart crond
systemctl is-enabled crond
[root@shu-test init.d]# systemctl is-enabled crond
enabled
[root@shu-test init.d]#
ls /usr/lib/systemd/system
service:系统服务;
target:多个unit组成的组;
device:硬件设备;
mount:系统挂载点;
automount:自动挂载点;
path:文件或路径;
scope:不是由systemd启动的外部进程;
slice:进程组;
snapshot:systemd快照;
socket:进程间通信套接字;
swap:swap文件;
timer:定时器;
systemctl list-units
列出所有的unit
systemctl list-units --all
列出所有inactive的unit
systemctl list-units --all --state=inactive
列出所有状态的service
systemctl list-units --all --type=service
列出状态为active的service
systemctl list-units --type=service
查看某个unit是否active
systemctl is-active crond.service
target类似于centos6里面启动级别,target支持多个target同时启动。target其实是多个unit的组合;
systemctl list-unit-files --type=target
systemctl list-dependencies multi-user.target
systemctl get-default
[root@shu-test init.d]# systemctl get-default
multi-user.target
[root@shu-test init.d]#
systemctl set-default multi-user.target
一个service属于一种类型的unit
多个unit组成一个target
一个target里面包含了多个service
查看服务文件
[root@shu-test init.d]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@shu-test init.d]#
任务计划crontab、服务管理(chkconfig、systemd)
标签:硬件 进程间 -name rand path 级别 cat 进程间通信 自定义服务
原文地址:http://blog.51cto.com/shuzonglu/2066587