Linux系統的任務計劃、系統服務、服務管理

[toc]html

Linux系統的任務計劃

擴展(未完成)node

  1. anacron http://blog.csdn.net/strikers1982/article/details/4787226linux

  2. xinetd服(默認機器沒有安裝這個服務,須要yum install xinetd安裝) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.htmlshell

  3. systemd自定義啓動腳本 http://www.jb51.net/article/100457.htmvim

10.23 任務計劃命令cron

1.cat /etc/crontab,查看到的數字左到右依次爲:分、時、日、月、周和命令行

[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

2.用crontab -e來編寫任務計劃,示例天天凌晨三點執行命令

[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

mark

mark

3.分範圍0-59,時範圍0-23,日範圍1-31,月範圍1-12,周1-7;可用格式1-5表示一個範圍1到5;可用格式1,2,3表示1或者2或者3; 可用格式*/2表示被2整除的數字,好比小時,那就是每隔2小時

4.要保證服務是啓動狀態-systemctl start crond

[root@localhost ~]# systemctl start crond
[root@localhost ~]# ps aux |grep cron
root        903  0.0  0.0 126264  1636 ?        Ss   15:16   0:00 /usr/sbin/crond -n
root       6761  0.0  0.0 112676   976 pts/0    S+   20:23   0:00 grep --color=auto cron

4.1 ps aux |grep cron查看是否啓動

4.2 systemctl status crond 方法二

[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 一 2018-02-26 10:27:29 CST; 9h ago
 Main PID: 903 (crond)
   CGroup: /system.slice/crond.service
           └─903 /usr/sbin/crond -n

4.3 如何中止計劃 -systemctl stop crond

5.任務計劃未執行的狀況

查看crontab -e中是否爲寫明命令的絕對路徑,而是直接寫了command, 同時把正確和錯誤的執行過程記錄在日誌文件中,==方便後續查找??是否說的這個==bash

mark

6.查看已經設定的任務計劃crontab -l

[root@localhost ~]# crontab -l
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

7.crontab-r刪除任務計劃

[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root

8.crontab -u 指定用戶

[root@localhost ~]# crontab -u root -l
no crontab for root

10.24 chkconfig系統服務工具(CentOS6)

1.chkconfig --list只剩下netconsole和network兩個服務了

[root@localhost ~]# 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:關

早期的CentOS6用到的服務管理都是SysV,而7換成了systemd. 輸入top,查看: markapp

2.查看Linux全部的預設服務:ls /etc/init.d/

[root@localhost ~]# ls /etc/init.d/
functions  netconsole  network  README

3.chkconfig network off/on

mark

4.回顧這裏7個系統啓動級別的知識,如今CnetOS7已經再也不嚴格區分,但6以前是有規定的:0即shutdown,1做爲重啓至單用戶,2表示無NFS服務支持的多用戶模式,3表示徹底多用戶模式,但不帶圖形,4保留用戶自定義,5表示圖形登入模式,6表示重啓

5.更改某個級別下的狀態:chkconfig --level 3 network off

[root@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# chkconfig --list |grep network

mark

[root@localhost ~]# chkconfig --level 345 network off

mark

6.chkconfig能夠把某個服務(腳本)加入系統中

[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# ls
functions  netconsole  network  README

增長名稱爲123的服務ssh

[root@localhost init.d]# cp network 123
[root@localhost init.d]# ls -l
總用量 40
-rwxr-xr-x  1 root root  6643 2月  26 21:05 123
-rw-r--r--. 1 root root 15131 9月  12 2016 functions
-rwxr-xr-x. 1 root root  2989 9月  12 2016 netconsole
-rwxr-xr-x. 1 root root  6643 9月  12 2016 network
-rw-r--r--. 1 root root  1160 5月  26 2017 README

mark

6.1這裏咱們查看下文件123,vim 123

mark

7.刪除某個服務chkconfig --del 123

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

10.25 systemd服務管理

1. 列出系統全部的服務

[root@localhost init.d]# systemctl list-units --all --type=service
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  abrt-ccpp.service                                     loaded    active   exited  Install ABRT coredump hook
  abrt-oops.service                                     loaded    active   running ABRT kernel log watcher
  abrt-vmcore.service                                   loaded    inactive dead    Harvest vmcores for ABRT
  abrt-xorg.service                                     loaded    active   running ABRT Xorg log watcher
  abrtd.service                                         loaded    active   running ABRT Automated Bug Reporting Tool
  accounts-daemon.service                               loaded    inactive dead    Accounts Service
  alsa-restore.service                                  loaded    inactive dead    Save/Restore Sound Card State
  alsa-state.service                                    loaded    active   running Manage Sound Card State (restore and store)
● apparmor.service                                      not-found inactive dead    apparmor.service
  atd.service                                           loaded    active   running Job spooling tools
  auditd.service                                        loaded    active   running Security Auditing Service
  auth-rpcgss-module.service                            loaded    inactive dead    Kernel Module supporting RPCSEC_GSS
  avahi-daemon.service                                  loaded    active   running Avahi mDNS/DNS-SD Stack

2.幾個經常使用的服務相關的命令

systemctl enable crond.service //讓服務開機啓動

systemctl disable crond //不讓開機啓動

systemctl status crond //查看狀態

systemctl stop crond //中止服務

systemctl start crond //啓動服務

systemctl restart crond //重啓服務

systemctl is-enabled crond //檢查服務是否開機啓動

[root@localhost ~]# systemctl enable crond.service
[root@localhost ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

mark

10.26 unit介紹

1.ls /usr/lib/systemd/system //系統全部unit,分爲如下類型

[root@localhost ~]# ls /usr/lib/systemd/system
abrt-ccpp.service                        irqbalance.service                  runlevel3.target
abrtd.service                            iscsid.service                      runlevel3.target.wants
abrt-oops.service                        iscsid.socket                       runlevel4.target
abrt-pstoreoops.service                  iscsi.service                       runlevel4.target.wants
abrt-vmcore.service                      iscsi-shutdown.service              runlevel5.target
abrt-xorg.service                        iscsiuio.service                    runlevel5.target.wants
accounts-daemon.service                  iscsiuio.socket                     runlevel6.target
alsa-restore.service                     kdump.service                       saslauthd.service
alsa-state.service                       kexec.target                        selinux-policy-migrate-local-changes@.service
alsa-store.service                       kexec.target.wants                  serial-getty@.service
anaconda-direct.service                  kmod-static-nodes.service           shutdown.target
anaconda-nm-config.service               kpatch.service                      shutdown.target.wants
anaconda-noshell.service                 ksm.service                         sigpwr.target
anaconda-pre.service                     ksmtuned.service                    sleep.target
anaconda.service                         libstoragemgmt.service              -.slice
anaconda-shell@.service                  libvirtd.service                    slices.target
anaconda-sshd.service                    libvirt-guests.service              smartcard.target
anaconda.target                          lldpad.service                      smartd.service
anaconda-tmux@.service                   lldpad.socket                       sockets.target
arp-ethers.service                       local-fs-pre.target                 sockets.target.wants

2. 上述的文件能夠歸爲如下幾類:

- [ ] service 系統服務

- [ ] target 多個unit組成的組

- [ ] device 硬件設備

- [ ] mount 文件系統掛載點

- [ ] automount 自動掛載點

- [ ] path 文件或路徑

  • [ ] scope 不是由systemd啓動的外部進程
  • [ ] slice 進程組
  • [ ] snapshot systemd快照
  • [ ] socket 進程間通訊套接字
  • [ ] swap swap文件
  • [ ] timer 定時器

mark

3.unit相關的命令

systemctl list-units //列出正在運行的unit

mark

systemctl list-units --all //列出全部,包括失敗的或者inactive的

systemctl list-units --all --state=inactive //列出inactive的unit

systemctl list-units --type=service//列出狀態爲active的service

mark

systemctl is-active crond.service //查看某個服務是否爲active

systemctl is-enable crond.service //查看某個服務是否爲enable

[root@localhost system]# systemctl is-active crond.service
active

10.27 target介紹:系統爲了方便管理用target來管理unit

1.查看當前系統的全部target:systemctl list-unit-files --type=target

[root@localhost ~]# systemctl list-unit-files --type=target
UNIT FILE                 STATE   
anaconda.target           static  
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  
hybrid-sleep.target       static  
initrd-fs.target          static  
initrd-root-fs.target     static  
initrd-switch-root.target static  
initrd.target             static  
iprutils.target           disabled
kexec.target              disabled
local-fs-pre.target       static  
local-fs.target           static  
machines.target           disabled
multi-user.target         enabled

2.systemctl list-dependencies multi-user.target //查看一個target包含的全部unit

[root@localhost ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─atd.service
● ├─auditd.service
● ├─avahi-daemon.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─cups.path
● ├─cups.service
● ├─dbus.service
● ├─irqbalance.service
● ├─kdump.service
● ├─ksm.service
● ├─ksmtuned.service
● ├─libstoragemgmt.service
● ├─libvirtd.service
● ├─mdmonitor.service
● ├─ModemManager.service
● ├─netcf-transaction.service
● ├─network.service

3.查看指定target下面有哪些unit:systemctl list-dependencies basic.target

[root@localhost ~]# systemctl list-dependencies basic.target
basic.target
● ├─alsa-restore.service
● ├─alsa-state.service
● ├─iptables.service
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
● ├─rhel-dmesg.service
● ├─rhel-loadmodules.service
● ├─selinux-policy-migrate-local-changes@targeted.service
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice
● ├─sockets.target
● │ ├─avahi-daemon.socket
● │ ├─cups.socket
● │ ├─dbus.socket
● │ ├─dm-event.socket
● │ ├─iscsid.socket
● │ ├─iscsiuio.socket
● │ ├─rpcbind.socket
● │ ├─systemd-initctl.socket
● │ ├─systemd-journald.socket

4. systemctl get-default //查看系統默認的target

[root@localhost ~]# systemctl get-default
multi-user.target

5.systemctl set-default multi-user.target //設置默認的target

[root@localhost ~]# 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.socket

6.多個unit組成了一個target;一個target裏面包含了多個service

7.查看sshd.service屬於哪一個target

cat /usr/lib/systemd/system/sshd.service //看[install]部分 mark工具

相關文章
相關標籤/搜索