【CentOS 7笔记46】,crondtab任务计划和chkconfig系统服务管理#
生活随笔
收集整理的這篇文章主要介紹了
【CentOS 7笔记46】,crondtab任务计划和chkconfig系统服务管理#
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
shallow丿ove
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
每天凌晨3點鐘執行指定腳本,并且將正確重定向或錯誤的重定向到指定文件日志里
[root@localhost ~]# crontab -e0 3 * * * /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log每隔兩個月的頭10天,并且這十天內剛好為周二和周五的時間執行指定腳本
[root@localhost ~]# crontab -e0 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 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.若沒有執行,很有可能是使用的命令而沒有用絕對路徑
查看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對應用戶的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 -lLinux系統服務管理-chkconfig
- chkconfig --list
- chkconfig --level 3 network off
- chkconfig --level 345 network off
- chkconfig --del network
- chkconfig --add network
列出服務
[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服務的進程
[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服務腳本
[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而這個級別為系統對應的七個級別
[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 --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添加服務
[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為運行級別,第10位啟動,第90位關閉
關閉服務
[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轉載于:https://my.oschina.net/u/3892756/blog/3056567
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的【CentOS 7笔记46】,crondtab任务计划和chkconfig系统服务管理#的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在 Android 上使用协程(二):G
- 下一篇: linux下查看cpu,内存,硬盘等硬件