您的位置:首页 > 其它

cron,chkconfig,systemd,unit,target介绍

2018-01-29 22:50 579 查看

cron任务计划



1.配置文件路径及模板:

[root@weix-01 ~]# 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

2.编辑该模板:

[root@weix-01 ~]# crontab -e
no crontab for root - using an empty one
0 3 1-10 */2 2,5  /bin/bash /usr/local/123.sh >> /tmp/123.log 2>> /tmp/123.log

3.启动服务并查看:

[root@weix-01 ~]# systemctl start crond
[root@weix-01 ~]# ps aux |grep crond
root        564  0.0  0.1 126236  1636 ?        Ss   16:18   0:00 /usr/sbin/cron d -n
root       1051  0.0  0.0 112676   984 pts/0    S+   16:27   0:00 grep --color=a uto crond
[root@weix-01 ~]# 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:18:19 CST; 9min ago
Main PID: 564 (crond)
CGroup: /system.slice/crond.service
└─564 /usr/sbin/crond -n

1月 29 16:18:19 weix-01 systemd[1]: Started Command Scheduler.
1月 29 16:18:19 weix-01 systemd[1]: Starting Command Scheduler...
1月 29 16:18:19 weix-01 crond[564]: (CRON) INFO (RANDOM_DELAY will be scal....)
1月 29 16:18:19 weix-01 crond[564]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.

4.常用用法:

[root@weix-01 ~]# crontab -e                   # -e编辑
no crontab for root - using an empty one
crontab: installing new crontab
[root@weix-01 ~]# corntab -l                  # -l 列出
-bash: corntab: 未找到命令
[root@weix-01 ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@weix-01 ~]# cat /var/spool/cron/root              #文件存放路径
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@weix-01 ~]# crontab -r                     # -r 删除
[root@weix-01 ~]# crontab -l
no crontab for root

chkconfig工具



1.列出所有服务:

[root@weix-01 ~]# 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:关

2.将指定服务开机启动或不启动:

[root@weix-01 ~]# chkconfig network off
[root@weix-01 ~]# 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@weix-01 ~]# chkconfig network on
[root@weix-01 ~]# 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:关

3.指定某个服务某个级别开关:

[root@weix-01 ~]# chkconfig --level 35 network off
[root@weix-01 ~]# 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@weix-01 ~]# chkconfig --level 35 network on
[root@weix-01 ~]# 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:关

4.自定义添加启动脚本:脚本要在init.d路径下,且文件格式有要求

[root@weix-01 ~]# cd /etc/init.d/
[root@weix-01 init.d]# cp network  123
[root@weix-01 init.d]# ls -l
总用量 48
-rwxr-xr-x. 1 root root  7293 1月  29 16:55 123
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 8月   5 14:38 README
[root@weix-01 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@weix-01 init.d]# chkconfig --add 123
[root@weix-01 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:关

#! /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

if [ ! -f /etc/sysconfig/network ]; then
exit 6
fi

. /etc/sysconfig/network

5.删除启动脚本:

[root@weix-01 init.d]# chkconfig --del 123
[root@weix-01 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:关

systemd



1.查看服务:

[root@weix-01 ~]# systemctl list-units --all --type=service
UNIT                        LOAD      ACTIVE   SUB     DESCRIPTION
auditd.service              loaded    active   running Security Auditing Servic
brandbot.service            loaded    inactive dead    Flexible Branding Servic
chronyd.service             loaded    active   running NTP client/server
cpupower.service            loaded    inactive dead    Configure CPU power rela
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
dm-event.service            loaded    inactive dead    Device-mapper event daem
dracut-shutdown.service     loaded    inactive dead    Restore /run/initramfs
ebtables.service            loaded    inactive dead    Ethernet Bridge Filterin
emergency.service           loaded    inactive dead    Emergency Shell
● exim.service                not-found inactive dead    exim.service

2.让服务开机启动,不启动:

[root@weixing01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@weixing01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

3.查看状态:

[root@weixing01 ~]# 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 22:21:00 CST; 4min 4s ago
Main PID: 542 (crond)
CGroup: /system.slice/crond.service
└─542 /usr/sbin/crond -n

1月 29 22:21:00 weixing01 systemd[1]: Started Command Scheduler.
1月 29 22:21:00 weixing01 systemd[1]: Starting Command Scheduler...
1月 29 22:21:00 weixing01 crond[542]: (CRON) INFO (RANDOM_DELAY will be s...)
1月 29 22:21:00 weixing01 crond[542]: (CRON) INFO (running with inotify s...)
Hint: Some lines were ellipsized, use -l to show in full.

4.停止,启动,重启服务:

[root@weixing01 ~]# systemctl stop crond
[root@weixing01 ~]# systemctl start crond
[root@weixing01 ~]# systemctl restart crond

5.查看服务是否开机启动:

[root@weixing01 ~]# systemctl is-enabled crond
enabled
[root@weixing01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@weixing01 ~]# systemctl is-enabled crond
disabled

unit



1.七个运行级别:

[root@weixing01 system]# ls -l runlevel*
lrwxrwxrwx. 1 root root 15 11月 15 02:02 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 11月 15 02:02 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 11月 15 02:02 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 11月 15 02:02 runlevel6.target -> reboot.target




target



1.查找target服务:

[root@weixing01 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

2.查看指定target下面有哪些unit:

[root@weixing01 system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.service
● ├─sshd.service
● ├─sysstat.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
● ├─vmtoolsd.service
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
lines 1-28

3.查看系统默认target:

[root@weixing01 system]# systemctl  get-default
multi-user.target

4.设置开机启动target:

[root@weixing01 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.

5.一个service属于一种类型unit,多个unit组成target,一个target里面包含多个service。
6.查看一个service属于哪个target:看install部分

[root@weixing01 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cron systemd unit
相关文章推荐