Systemctl (CentOS 7) - cruisechang/wiki-linux GitHub Wiki
Basic
systemctl start httpd
systemctl stop httpd
systemctl status httpd
systemctl enable httpd //建立連結,自動啟動
systemctl disable http //取消連結,無自動啟動
Setting Service
官方預設設定檔位置(啟動檔是連結到這裡,刪除連結檔就不會啟動) /usr/lib/systemd/system/
啟動連結檔位置 /etc/systemd/system/
- Example:
真正的設定檔:/usr/lib/systemd/system/vsftpd.service
複製的設定檔: /etc/systemd/system/vsftpd.service.d/custom.conf:
在 /etc/systemd/system 底下建立與設定檔相同檔名的目錄,要加上.d副目錄名。然後在該目錄下建立設定檔即可。
Setting File Example
設定檔基本分三部分
-
[Unit]
-
[Service], [Socket], [Timer], [Mount], [Path]..
-
[Install]
[root@study ~]# cat /usr/lib/systemd/system/sshd.service
[Unit] # 這個項目與此 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] # 這個項目說明此 unit 要掛載哪個 target 底下
WantedBy=multi-user.target
Self Service
- vim /etc/systemd/system/self.service (建立自訂設定檔)
[Unit]
Description=backup my server
Requires=atd.service //需要的其他服務
[Service]
Type=simple
WorkingDirectory=/yourdir/dir/ //working dir
ExecStart=/bin/bash -c " echo /backups/backup.sh | at now" //執行位置
[Install]
WantedBy=multi-user.target //通常用這個
- Execute service
Copy self.service to /etc/systemd/system/
[root@study ~]# systemctl daemon-reload
[root@study ~]# systemctl enable self.service (建立連結)
[root@study ~]# systemctl start self.service
[root@study ~]# systemctl status self.service
[root@study ~]# systemctl stop self.service