配置一個簡單服務,讓它在出現異常時自動重啓。vim
Ubuntu 16.04.2 LTSbash
爲方便觀察,服務功能設定爲:監視文件 /tmp/foo
,一旦該文件發生變化,同步到 /tmp/bar
。測試
sudo apt-get update sudo apt-get install inotify-tools
mkdir -p /usr/lib/systemd/system vim /usr/lib/systemd/system/foo.service
[Unit] Description=foo [Service] ExecStart=/bin/bash -c "while true; do /usr/bin/inotifywait -qq --event modify /tmp/foo; cp /tmp/foo /tmp/bar; done" Restart=always [Install] WantedBy=multi-user.target
touch /tmp/foo
systemctl start foo
echo hello>/tmp/foo && sleep 1 && cat /tmp/foo /tmp/bar
應該輸出兩個 hello
。code
ps aux | grep foo | grep bash
kill <pid>
ps aux | grep foo | grep bash
此時,pid
應該變了。進程
echo world>/tmp/foo && sleep 1 && cat /tmp/foo /tmp/bar
應該輸出兩個 world
。ip
systemctl stop foo
測試完畢。get