linux任务计划cron
crontab的配置文件
MAILTO表示发送邮件给谁
[root@localhost ~]# 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
进入编辑模式crontab -e
和vim用法一样
实例
[root@localhost ~]# crontab -e
(在0分3点,1到10号,能被2整除的月份,星期二和星期五执行123.sh脚本,记录值追加重定向到123.log,错位置追加重定向到123.log)
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
确保cron启动
[root@localhost ~]# systemctl start crond
[root@localhost ~]# ps aux |grep cron
root 519 0.0 0.0 126236 1600 ? Ss 04:28 0:00 /usr/sbin/crond -n
root 12712 0.0 0.0 112676 984 pts/0 S+ 12:59 0:00 grep --color=auto cron
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2018-03-16 04:28:03 CST; 8h ago
Main PID: 519 (crond)
CGroup: /system.slice/crond.service
└─519 /usr/sbin/crond -n
3月 16 04:28:03 localhost.localdomain systemd[1]: Started Command Sch...
3月 16 04:28:03 localhost.localdomain systemd[1]: Starting Command Sc...
3月 16 04:28:03 localhost.localdomain crond[519]: (CRON) INFO (RANDOM...
3月 16 04:28:03 localhost.localdomain crond[519]: (CRON) INFO (runnin...
Hint: Some lines were ellipsized, use -l to show in full.
crontab的选项
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -u root -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -u root -l
Linux系统服务管理-chkconfig
CentOS7已经不再使用,但是还可以使用,向之前版本兼容
0级别:关机状态
1级别:单用户
2级别:相比3少一个nfs服务
3级别:多用户模式,不带图形
4级别:保留级别,暂时没用
5级别:多用户带图形
6级别:重启
CentOS6就已经没有运行级别了
[root@localhost ~]# 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@localhost ~]# chkconfig network off
[root@localhost ~]# 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@localhost ~]# chkconfig network on
[root@localhost ~]# 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@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# 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@localhost ~]# chkconfig --level 35 network off
[root@localhost ~]# 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@localhost ~]# chkconfig --level 345 network on
[root@localhost ~]# 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@localhost ~]# cp /etc/init.d/network /etc/init.d/123
[root@localhost ~]# 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@localhost ~]# chkconfig --add 123
[root@localhost ~]# 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:关
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
[root@localhost init.d]# chkconfig --del 123
[root@localhost 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:关
启动脚本必须要在/etc/init.d/里面
必须包含下面两部分服务才能被chkconfig识别
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
systemd管理服务
所有服务
[root@localhost init.d]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1.scope static
session-10.scope static
session-16.scope static
session-2.scope static
session-4.scope static
session-5.scope static
arp-ethers.service disabled
auditd.service enabled
一般查看服务
[root@localhost init.d]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
brandbot.service loaded inactive dead Flexible Branding Service
cpupower.service loaded inactive dead Configure CPU power related settings
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
● display-manager.service not-found inactive dead display-manager.service
dracut-shutdown.service loaded inactive dead Restore /run/initramfs
ebtables.service loaded inactive dead Ethernet Bridge Filtering tables
emergency.service loaded inactive dead Emergency Shell
● exim.service not-found inactive dead exim.service
firewalld.service loaded active running firewalld - dynamic firewall daemon
getty@tty1.service loaded active running Getty on tty1
ip6tables.service loaded inactive dead IPv6 firewall with ip6tables
● ipset.service not-found inactive dead ipset.service
iptables.service loaded inactive dead IPv4 firewall with iptables
irqbalance.service loaded inactive dead irqbalance daemon
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device no
microcode.service loaded inactive dead Load CPU microcode update
network.service loaded active exited LSB: Bring up/down networking
● NetworkManager-wait-online.service loaded failed failed Network Manager Wait Online
NetworkManager.service loaded inactive dead Network Manager
plymouth-quit-wait.service loaded inactive dead Wait for Plymouth Boot Screen to Quit
plymouth-quit.service loaded inactive dead Terminate Plymouth Boot Screen
plymouth-read-write.service loaded inactive dead Tell Plymouth To Write Out Runtime Data
plymouth-start.service loaded inactive dead Show Plymouth Boot Screen
polkit.service loaded active running Authorization Manager
postfix.service loaded active running Postfix Mail Transport Agent
rc-local.service loaded inactive dead /etc/rc.d/rc.local Compatibility
rescue.service loaded inactive dead Rescue Shell
rhel-autorelabel-mark.service loaded inactive dead Mark the need to relabel after reboot
rhel-autorelabel.service loaded inactive dead Relabel all filesystems, if necessary
rhel-configure.service loaded inactive dead Reconfigure the system on administrator
rhel-dmesg.service loaded active exited Dump dmesg to /var/log/dmesg
rhel-import-state.service loaded active exited Import network configuration from initra
rhel-loadmodules.service loaded inactive dead Load legacy module configuration
rhel-readonly.service loaded active exited Configure read-only root support
rsyslog.service loaded active running System Logging Service
selinux-policy-migrate-local-changes@targeted.service loaded inactive dead Migrate local SELinux policy
● sendmail.service not-found inactive dead sendmail.service
sshd-keygen.service loaded inactive dead OpenSSH Server Key Generation
sshd.service loaded active running OpenSSH server daemon
● syslog.service not-found inactive dead syslog.service
systemd-ask-password-console.service loaded inactive dead Dispatch Password Requests to Console
systemd-ask-password-plymouth.service loaded inactive dead Forward Password Requests to Plymouth
systemd-ask-password-wall.service loaded inactive dead Forward Password Requests to Wall
systemd-binfmt.service loaded inactive dead Set Up Additional Binary Formats
systemd-firstboot.service loaded inactive dead First Boot Wizard
systemd-fsck-root.service loaded active exited File System Check on Root Device
systemd-fsck@dev-disk-by\x2duuid-19803e96\x2d02eb\x2d4a1a\x2d97d8\x2d270a8b891a47.service loaded active e
systemd-hwdb-update.service loaded inactive dead Rebuild Hardware Database
systemd-initctl.service loaded inactive dead /dev/initctl Compatibility Daemon
systemd-journal-catalog-update.service loaded inactive dead Rebuild Journal Catalog
systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-machine-id-commit.service loaded inactive dead Commit a transient machine-id on disk
systemd-modules-load.service loaded inactive dead Load Kernel Modules
● systemd-random-seed-load.service not-found inactive dead systemd-random-seed-load.service
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-readahead-collect.service loaded inactive dead Collect Read-Ahead Data
systemd-readahead-done.service loaded inactive dead Stop Read-Ahead Data Collection
systemd-readahead-replay.service loaded inactive dead Replay Read-Ahead Data
systemd-reboot.service loaded inactive dead Reboot
systemd-remount-fs.service loaded active exited Remount Root and Kernel File Systems
systemd-shutdownd.service loaded inactive dead Delayed Shutdown Service
systemd-sysctl.service loaded active exited Apply Kernel Variables
● systemd-sysusers.service not-found inactive dead systemd-sysusers.service
systemd-tmpfiles-clean.service loaded inactive dead Cleanup of Temporary Directories
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-done.service loaded inactive dead Update is Completed
systemd-update-utmp-runlevel.service loaded inactive dead Update UTMP about System Runlevel Change
systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tuned.service loaded active running Dynamic System Tuning Daemon
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
80 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
让开机启动
不让开机启动
查看状态
停止服务
启动服务
重启服务
检查服务是否开机启动
[root@localhost init.d]# systemctl enable crond.service
[root@localhost init.d]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@localhost init.d]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
Active: active (running) since 五 2018-03-16 04:28:03 CST; 9h ago
Main PID: 519 (crond)
CGroup: /system.slice/crond.service
└─519 /usr/sbin/crond -n
3月 16 04:28:03 localhost.localdomain systemd[1]: Started Command Scheduler.
3月 16 04:28:03 localhost.localdomain systemd[1]: Starting Command Scheduler...
3月 16 04:28:03 localhost.localdomain crond[519]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 4...ed.)
3月 16 04:28:03 localhost.localdomain crond[519]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost init.d]# systemctl stop crond.service
[root@localhost init.d]# systemctl start crond.service
[root@localhost init.d]# systemctl restart crond.service
[root@localhost init.d]# systemctl is-enabled crond.service
disabled
unit介绍
[root@localhost system]# ls -l /usr/lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 3月 14 05:49 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 3月 14 05:49 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 3月 14 05:49 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 3月 14 05:49 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 3月 14 05:49 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 3月 14 05:49 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 3月 14 05:49 /usr/lib/systemd/system/runlevel6.target -> reboot.target
/usr/lib/systemd/system/runlevel1.target.wants:
总用量 0
lrwxrwxrwx. 1 root root 39 3月 14 05:49 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel2.target.wants:
总用量 0
lrwxrwxrwx. 1 root root 39 3月 14 05:49 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel3.target.wants:
总用量 0
lrwxrwxrwx. 1 root root 39 3月 14 05:49 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel4.target.wants:
总用量 0
lrwxrwxrwx. 1 root root 39 3月 14 05:49 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
/usr/lib/systemd/system/runlevel5.target.wants:
总用量 0
lrwxrwxrwx. 1 root root 39 3月 14 05:49 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
target介绍
[root@localhost system]# systemctl list-unit-files --type=target
UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static
ctrl-alt-del.target disabled
default.target enabled
emergency.target static
final.target static
getty.target static
graphical.target static
halt.target disabled
hibernate.target static
hybrid-sleep.target static
initrd-fs.target static
initrd-root-fs.target static
initrd-switch-root.target static
initrd.target static
iprutils.target disabled
kexec.target disabled
local-fs-pre.target static
local-fs.target static
machines.target disabled
multi-user.target enabled
network-online.target static
network-pre.target static
network.target static
nss-lookup.target static
nss-user-lookup.target static
paths.target static
poweroff.target disabled
printer.target static
reboot.target disabled
remote-fs-pre.target static
remote-fs.target enabled
rescue.target disabled
rpcbind.target static
runlevel0.target disabled
runlevel1.target disabled
runlevel2.target enabled
runlevel3.target enabled
runlevel4.target enabled
runlevel5.target static
runlevel6.target disabled
shutdown.target static
sigpwr.target static
sleep.target static
slices.target static
smartcard.target static
sockets.target static
sound.target static
suspend.target static
swap.target static
sysinit.target static
system-update.target static
time-sync.target static
timers.target static
umount.target static
57 unit files listed.
[root@localhost system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─avahi-daemon.service
● ├─brandbot.path
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.service
● ├─sshd.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
● ├─systemd-readahead-collect.service
● ├─systemd-readahead-replay.service
● ├─systemd-update-utmp-runlevel.service
● ├─systemd-user-sessions.service
● ├─tuned.service
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
● │ ├─rhel-autorelabel.service
● │ ├─rhel-configure.service
● │ ├─rhel-dmesg.service
● │ ├─rhel-loadmodules.service
● │ ├─selinux-policy-migrate-local-changes@targeted.service
● │ ├─paths.target
● │ ├─slices.target
● │ │ ├─-.slice
● │ │ └─system.slice
● │ ├─sockets.target
● │ │ ├─avahi-daemon.socket
● │ │ ├─dbus.socket
● │ │ ├─systemd-initctl.socket
● │ │ ├─systemd-journald.socket
● │ │ ├─systemd-shutdownd.socket
● │ │ ├─systemd-udevd-control.socket
● │ │ └─systemd-udevd-kernel.socket
● │ ├─sysinit.target
● │ │ ├─dev-hugepages.mount
● │ │ ├─dev-mqueue.mount
● │ │ ├─kmod-static-nodes.service
● │ │ ├─plymouth-read-write.service
● │ │ ├─plymouth-start.service
● │ │ ├─proc-sys-fs-binfmt_misc.automount
● │ │ ├─sys-fs-fuse-connections.mount
● │ │ ├─sys-kernel-config.mount
● │ │ ├─sys-kernel-debug.mount
● │ │ ├─systemd-ask-password-console.path
● │ │ ├─systemd-binfmt.service
● │ │ ├─systemd-firstboot.service
● │ │ ├─systemd-hwdb-update.service
● │ │ ├─systemd-journal-catalog-update.service
● │ │ ├─systemd-journal-flush.service
● │ │ ├─systemd-journald.service
● │ │ ├─systemd-machine-id-commit.service
● │ │ ├─systemd-modules-load.service
● │ │ ├─systemd-random-seed.service
● │ │ ├─systemd-sysctl.service
● │ │ ├─systemd-tmpfiles-setup-dev.service
● │ │ ├─systemd-tmpfiles-setup.service
● │ │ ├─systemd-udev-trigger.service
● │ │ ├─systemd-udevd.service
● │ │ ├─systemd-update-done.service
● │ │ ├─systemd-update-utmp.service
● │ │ ├─systemd-vconsole-setup.service
● │ │ ├─cryptsetup.target
● │ │ ├─local-fs.target
● │ │ │ ├─-.mount
● │ │ │ ├─boot.mount
● │ │ │ ├─rhel-import-state.service
● │ │ │ ├─rhel-readonly.service
● │ │ │ ├─systemd-fsck-root.service
● │ │ │ └─systemd-remount-fs.service
● │ │ └─swap.target
● │ │ └─dev-disk-by\x2duuid-2cd465a5\x2dd2b7\x2d4135\x2da0f6\x2d4b6dba45385a.swap
● │ └─timers.target
● │ └─systemd-tmpfiles-clean.timer
● ├─getty.target
● │ └─getty@tty1.service
● └─remote-fs.target
[root@localhost system]# systemctl get-default
multi-user.target
[root@localhost system]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@localhost system]# 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
(属于multi-user.target)
扩展
1. anacron http://blog.csdn.net/strikers1982/article/details/4787226
2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定义启动脚本 http://www.jb51.net/article/100457.htm