linux任務計劃cron chkconfig工具 systemd管理服務 unit介紹 target介紹
[root@lizhipenglinux01 ~]# crontab -e 編寫任務計劃
no crontab for root - using an empty one
20 11 29 01 1 echo "OK" > /root/cron.log 表示在1月29日(星期一)的11點20分執行:echo "OK" > /root/cron.log
命令crontab -e 實際上打開/var/spool/cron/username文件(如果用戶是root,則打開的是/var/spool/cron/root)。打開這個文件使用了vim編輯器,保存時命令模式下輸入:wq即可。
[root@lizhipenglinux01 ~]# crontab -l 查看已經設定的任務計劃
20 11 29 01 1 echo "OK" > /root/cron.log
[root@lizhipenglinux01 ~]# crontab -r 刪除任務計劃,慎用。它會一下子把全部計劃都刪除。如果只想刪除一條計劃,可以使用-e選項進入crontab進行編輯。
[root@lizhipenglinux01 ~]# crontab -l
no crontab for root
(1)每天淩晨1點20分清除/var/log/slow.log這個文件
20 1 * * * echo "" > /var/log/slow.log
(2)每周日3點執行/bin/sh /usr/local/sbin/backup.sh。
0 3 * * 0 /bin/sh /usr/local/sbin/backup.sh
(3)每月14日4點10分執行/bin/sh /usr/local/sbin/backup_month.sh。
10 4 14 * * 、bin/sh /usr/local/sbin/backup_month.sh
(4)每隔8小時執行ntpdate time.windows.com
* */8 * * * ntpdate time.windows.com
(5)每天1點、12點、18點執行/bin/sh /usr/local/sbin/test.sh。
0 1,12,18 * * * /bin/sh /usr/local/sbin/test.sh
(6)每天的9點到18點執行/bin/sh /usr/local/sbin/test2.sh。
0 9-18 * * * /bin/sh /usr/local/sbin/test2.sh
查看一下crond服務是否已經啟動,看Active,如果是啟動狀態顯示為active(runing),未啟動則顯示inacvtive(dead)。
[root@lizhipenglinux01 ~]# ls /etc/init.d/
functions netconsole network README
[root@lizhipenglinux01 ~]# chkconfig network off 關閉network
[root@lizhipenglinux01 ~]# 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@lizhipenglinux01 ~]# chkconfig network on
[root@lizhipenglinux01 ~]# 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@lizhipenglinux01 ~]# chkconfig --level 345 network on
[root@lizhipenglinux01 ~]# 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@lizhipenglinux01 init.d]# chkconfig --del 123
[root@lizhipenglinux01 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@lizhipenglinux01 init.d]# systemctl enable crond.service
[root@lizhipenglinux01 init.d]# systemctl disable crond
rm ‘/etc/systemd/system/multi-user.target.wants/crond.service‘
[root@lizhipenglinux01 init.d]# systemctl enable crond.service
ln -s ‘/usr/lib/systemd/system/crond.service‘ ‘/etc/systemd/system/multi-user.target.wants/crond.service‘
[root@lizhipenglinux01 init.d]# systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: active (running) since 日 2018-01-28 18:18:32 CST; 2h 48min ago
Main PID: 861 (crond)
CGroup: /system.slice/crond.service
└─861 /usr/sbin/crond -n
1月 28 18:18:32 lizhipenglinux01 systemd[1]: Started Command Scheduler.
1月 28 18:18:32 lizhipenglinux01 crond[861]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 34% if used.)
1月 28 18:18:32 lizhipenglinux01 crond[861]: (CRON) INFO (running with inotify support)
1月 28 19:07:55 lizhipenglinux01 systemd[1]: Started Command Scheduler.
[root@lizhipenglinux01 init.d]# systemctl disable crond
rm ‘/etc/systemd/system/multi-user.target.wants/crond.service‘
[root@lizhipenglinux01 init.d]# systemctl is-enabled crond
disabled
[root@lizhipenglinux01 init.d]# systemctl enable crond
ln -s ‘/usr/lib/systemd/system/crond.service‘ ‘/etc/systemd/system/multi-user.target.wants/crond.service‘
軟鏈接
[root@lizhipenglinux01 system]# systemctl list-units -all --state=inactive 列出所有狀態inactive的units
[root@lizhipenglinux01 system]# systemctl list-units -all --type=service
[root@lizhipenglinux01 system]# systemctl is-active crond.service
active
[root@lizhipenglinux01 system]# systemctl is-enabled crond.service
enabled
[root@lizhipenglinux01 system]# systemctl list-unit-files --type=target 列出系統裏所有的target
[root@lizhipenglinux01 system]# systemctl get-default
multi-user.target
[root@lizhipenglinux01 system]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
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@lizhipenglinux01 system]# cat /usr/lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
[Install]
WantedBy=multi-user.target
linux任務計劃cron chkconfig工具 systemd管理服務 unit介紹 target介紹