配置一个简单服务,让它在出现异常时自动重启。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