hellopasswd
Linux任务计划
- crontab -u、-e、-l、-r
- 格式:分 时 日 月 周 user command
- 文件/var/spool/cron/username
- 分范围0-59,时范围0-23,日范围0-31,月范围1-12,周0-6
- 可用格式1-5表示一个范围1到5
- 可用格式1,2,3表示1或者2或者3
- 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
- 要保证服务是启动状态
- systemctl start crond.service
[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
每天凌晨3点钟执行指定脚本,并且将正确重定向或错误的重定向到指定文件日志里
[root@localhost ~]# crontab -e
0 3 * * * /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log
每隔两个月的头10天,并且这十天内刚好为周二和周五的时间执行指定脚本
[root@localhost ~]# crontab -e
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log
启动服务
[root@localhost ~]# systemctl start crond
[root@localhost ~]# systemctl start crond.service
[root@localhost ~]# ps aux | grep crond
root 729 0.0 0.0 126344 1608 ? Ss Dec04 0:00 /usr/sbin/crond -n
root 3014 0.0 0.0 112656 976 pts/0 S+ 00:00 0:00 grep --color=auto crond
active (running)
[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 Mon 2017-12-04 23:40:29 CST; 20min ago
Main PID: 729 (crond)
CGroup: /system.slice/crond.service
└─729 /usr/sbin/crond -n
Dec 04 23:40:29 localhost.localdomain systemd[1]: Started Command Scheduler.
Dec 04 23:40:29 localhost.localdomain systemd[1]: Starting Command Scheduler...
Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (RANDOM_DELAY will be scaled wit....)
Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (running with inotify support)
Dec 05 00:01:01 localhost.localdomain crond[729]: (root) RELOAD (/var/spool/cron/root)
Hint: Some lines were ellipsized, use -l to show in full.
inactive (dead)
[root@localhost ~]# systemctl stop crond
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2017-12-05 00:02:09 CST; 1s ago
Process: 729 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=exited, status=0/SUCCESS)
Main PID: 729 (code=exited, status=0/SUCCESS)
Dec 04 23:40:29 localhost.localdomain systemd[1]: Started Command Scheduler.
Dec 04 23:40:29 localhost.localdomain systemd[1]: Starting Command Scheduler...
Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (RANDOM_DELAY will be scaled wit....)
Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (running with inotify support)
Dec 05 00:01:01 localhost.localdomain crond[729]: (root) RELOAD (/var/spool/cron/root)
Dec 05 00:02:09 localhost.localdomain systemd[1]: Stopping Command Scheduler...
Dec 05 00:02:09 localhost.localdomain systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
若没有执行,很有可能是使用的命令而没有用绝对路径
查看crondtab
[root@localhost ~]# crontab -l
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log
对应用户的crondtab执行脚本
[root@localhost ~]# cat /var/spool/cron/root
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log
若要备份可直接备份/var/spool/cron/root的文件
删除crondtab
[root@localhost ~]# crontab -r
指定用户
[root@localhost ~]# crondtab -u root -l
Linux系统服务管理-chkconfig
- chkconfig --list
- chkconfig --level 3 network off
- chkconfig --level 345 network off
- chkconfig --del network
- chkconfig --add network
列出服务
[root@localhost ~]# chkconfig
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
systemd服务的进程
[root@localhost ~]# top
top - 02:52:51 up 3:12, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 365 total, 1 running, 364 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1870784 total, 277448 used, 1593336 free, 692 buffers
KiB Swap: 2097148 total, 0 used, 2097148 free. 100316 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
349 root 20 0 0 0 0 S 0.3 0.0 0:09.63 kworker/0:3
1 root 20 0 41236 3768 2432 S 0.0 0.2 0:01.46 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.06 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.13 kworker/u256:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/1
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/2
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/3
服务脚本
[root@localhost ~]# ls /etc/init.d/
functions iprdump iprinit iprupdate netconsole network README
[root@localhost ~]# chkconfig network off
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost ~]# chkconfig network on
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
而这个级别为系统对应的七个级别
[root@localhost ~]# vi /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
~
指定级别
[root@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:off 4:on 5:on 6:off
[root@localhost ~]# chkconfig --level 345 network on
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
添加服务
[root@localhost ~]# cp /etc/init.d/network /etc/init.d/123
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]# chkconfig --add /etc/init.d/123
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
123 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]# vi /etc/init.d/123
#! /bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO
# Source function library.
. /etc/init.d/functions
.
.
.
chkconfig: 2345 10 90 1234为运行级别,第10位启动,第90位关闭
关闭服务
[root@localhost ~]# chkconfig --del /etc/init.d/123
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改于 171204