Linux中创建systemd service
目录
方法:
-
在
/etc/systemd/system/
下创建一个以“服务名.service”命名的文本文件,内容如下:[Unit] Description=<description about this service> [Service] User=<user e.g. root> WorkingDirectory=<directory_of_script e.g. /root> ExecStart=<script which needs to be executed> Restart=always [Install] WantedBy=multi-user.target
比如我创建的一个更新博客的服务
blog-updater.service
:[Unit] Description=update blog from git repository [Service] User=updater WorkingDirectory=/blog/ ExecStart=/blog/updater Restart=always [Install] WantedBy=multi-user.target
-
重新加载服务文件以包含新建立的那个服务
$sudo systemctl daemon-reload
-
启动服务
$sudo systemctl start your-service.service
-
每次重启后,自动启动该服务
$sudo systemctl enable your-service.service
想了解更详细的服务配置文件的字段含义,请参考《systemd 入门教程》。
参考
阮一峰的《systemd 入门教程》:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
《How to create a Systemd service in Linux》:https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/