systemd oneshot服务配置例子

 

服务这样写:

复制代码
[Unit]
Description=nginx - high performance web server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=oneshot                                        // 类型选 oneshot
RemainAfterExit=yes                                 // 退出之后保持active状态:active (exited) 没有的话,结束状态是:inactive (dead)
ExecStart=/usr/bin/bash /root/test1.sh
ExecStart=/usr/bin/bash /root/test2.sh              // test2.sh 在 test1.sh 结束后,并且允许成功的时候,才执行。

StandardOutput=append:/var/Logs/test_init.log // 不加也行,systemctl show xxx.server 可以看见默认配置
StandardError=append:/var/Logs/test_init.log // 不加也行,systemctl show xxx.server 可以看见默认配置
[Install]
WantedBy=multi-user.target
复制代码

 

test1.sh 如果 exit(1), test2.sh 不会执行,并且服务状态是failed。

test1.sh 如果 exit(0-),test2.sh 继续执行,如果exit(1) 服务状态是failed, 否则是active。

 

如果在这个服务之后有依赖程序的话,RemainAfterExit=yes,配置很重要。否则不是active状态,后续的依赖程序不会执行。

例如,我的nginx程序,对这个服务强依赖,可以如下写:

复制代码
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target test-init.service    // 这里必须有
Wants=network-online.target
Requisite=test-init.service                  // 强依赖

[Service]
LimitNOFILE=2048000
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
复制代码

 

posted on   toong  阅读(73)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2017-10-25 [daily][btrfs][mlocate][updatedb] mlocate不认识btrfs里面的文件

统计

点击右上角即可分享
微信分享提示