【CentOS 7笔记46】,crondtab任务计划和chkconfig系统服务管理#
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
shallow丿ove
Linux任務(wù)計(jì)劃
- crontab -u、-e、-l、-r
- 格式:分 時(shí) 日 月 周 user command
- 文件/var/spool/cron/username
- 分范圍0-59,時(shí)范圍0-23,日范圍0-31,月范圍1-12,周0-6
- 可用格式1-5表示一個(gè)范圍1到5
- 可用格式1,2,3表示1或者2或者3
- 可用格式*/2表示被2整除的數(shù)字,比如小時(shí),那就是每隔2小時(shí)
- 要保證服務(wù)是啟動(dòng)狀態(tài)
- systemctl start crond.service
每天凌晨3點(diǎn)鐘執(zhí)行指定腳本,并且將正確重定向或錯(cuò)誤的重定向到指定文件日志里
[root@localhost ~]# crontab -e0 3 * * * /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log每隔兩個(gè)月的頭10天,并且這十天內(nèi)剛好為周二和周五的時(shí)間執(zhí)行指定腳本
[root@localhost ~]# crontab -e0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log啟動(dòng)服務(wù)
[root@localhost ~]# systemctl start crond [root@localhost ~]# systemctl start crond.service [root@localhost ~]# ps aux | grep crondroot 729 0.0 0.0 126344 1608 ? Ss Dec04 0:00 /usr/sbin/crond -nroot 3014 0.0 0.0 112656 976 pts/0 S+ 00:00 0:00 grep --color=auto crondactive (running)
[root@localhost ~]# systemctl status crond● crond.service - Command SchedulerLoaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2017-12-04 23:40:29 CST; 20min agoMain PID: 729 (crond)CGroup: /system.slice/crond.service└─729 /usr/sbin/crond -nDec 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 SchedulerLoaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)Active: inactive (dead) since Tue 2017-12-05 00:02:09 CST; 1s agoProcess: 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.若沒(méi)有執(zhí)行,很有可能是使用的命令而沒(méi)有用絕對(duì)路徑
查看crondtab
[root@localhost ~]# crontab -l0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log對(duì)應(yīng)用戶(hù)的crondtab執(zhí)行腳本
[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指定用戶(hù)
[root@localhost ~]# crondtab -u root -lLinux系統(tǒng)服務(wù)管理-chkconfig
- chkconfig --list
- chkconfig --level 3 network off
- chkconfig --level 345 network off
- chkconfig --del network
- chkconfig --add network
列出服務(wù)
[root@localhost ~]# chkconfigNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:offsystemd服務(wù)的進(jìn)程
[root@localhost ~]# toptop - 02:52:51 up 3:12, 3 users, load average: 0.00, 0.01, 0.05Tasks: 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 stKiB Mem: 1870784 total, 277448 used, 1593336 free, 692 buffersKiB Swap: 2097148 total, 0 used, 2097148 free. 100316 cached MemPID 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服務(wù)腳本
[root@localhost ~]# ls /etc/init.d/functions iprdump iprinit iprupdate netconsole network README [root@localhost ~]# chkconfig network off [root@localhost ~]# chkconfig --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@localhost ~]# chkconfig network on [root@localhost ~]# chkconfig --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:off而這個(gè)級(jí)別為系統(tǒng)對(duì)應(yīng)的七個(gè)級(jí)別
[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#~指定級(jí)別
[root@localhost ~]# chkconfig --level 3 network off [root@localhost ~]# chkconfig --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:off 4:on 5:on 6:off [root@localhost ~]# chkconfig --level 345 network on [root@localhost ~]# chkconfig --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:off添加服務(wù)
[root@localhost ~]# cp /etc/init.d/network /etc/init.d/123 [root@localhost ~]# chkconfig --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 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 --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 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為運(yùn)行級(jí)別,第10位啟動(dòng),第90位關(guān)閉
關(guān)閉服務(wù)
[root@localhost ~]# chkconfig --del /etc/init.d/123 [root@localhost ~]# chkconfig --listNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd 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:offiprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:offiprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:off轉(zhuǎn)載于:https://my.oschina.net/u/3892756/blog/3056567
超強(qiáng)干貨來(lái)襲 云風(fēng)專(zhuān)訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的【CentOS 7笔记46】,crondtab任务计划和chkconfig系统服务管理#的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在 Android 上使用协程(二):G
- 下一篇: 系统功能的可配置性设想