1、用法:systemctl [OPT] COMMAND [NAME]…
启动服务:systemctl start name.service
停止服务:systemctl stop name.service
重启服务:systemctl restart name.service
服务状态:systemctl status name.service
服务重载:systemctl daemon-reload
条件式重启,已启动才重启,否则不作操作:systemctl tryrestart name.service
重载或重启服务,先加载再启动:systemctl reloadorrestart name.service
重载或条件式重启:systemctl reloadortryrestart name.service
禁止自动和手动启动:systemctl mask name.service
取消禁止:systemctl unmask name.service
查看某服务当前激活与否的状态:systemctl isactive name.service
查看所有已经激活的服务:systemctl listunits t service
查看所有服务:systemctl listunits t service a
设定某服务开机自启动:systemctl enable name.service
设定某服务开机禁止启动:systemctl disable name.service
查看所有服务的开机自启动状态:systemctl listunitfiles –t service
列出该服务在哪些运行级别下启用和禁止:ls /etc/systemd/system/*.wants/sshd.service
查看服务是否开机启动:systemctl isenabled name.service
查看服务的依赖关系:systemctl listdependencies name.service
杀掉进程:systemctl kill (进程名)
2、服务状态:
loaded:unit配置文件已处理
active(running):一次或多次持续处理的运行
active(exited):成功完成一次性配置
active(waiting):运行中,等待一个事件
inactive:不运行
enable:开机启动
disable:开机不启动
static:开机不启动,但可以被另一个启用的服务激活。
一、QT实现后台服务
QT使用开源项目QtService实现后台服务,支持windows和linux跨平台使用,下载地址:
https://github.com/qtproject/qt-solutions/tree/master/qtservice
QT新建控制台程序"Qt Console Application",把qtservice文件夹拷贝到QT工程目录,在pro文件添加:
include(qtservice/src/qtservice.pri)
1
添加新文件:ubuntuservice.h和ubuntuservice.cpp,服务功能主要由重写的几个虚函数完成:start、stop、pause、resume,在启动、停止、暂停、重启服务时会自动调用这些虚函数。
下面的demo用QProcess启动停止外部程序,功能在外部程序实现(test_systemd),这里外部程序的功能是启动一个TCP客户端,连接到服务器,当收到服务器的消息时自动回复一条confirm消息。
修改main.cpp
#include "ubuntuservice.h"
int main(int argc, char *argv[])
{
ubuntuService service(argc, argv);
return service.exec();
}
1
2
3
4
5
6
ubuntuservice.h
#ifndef UBUNTUSERVICE_H
#define UBUNTUSERVICE_H
#include <QCoreApplication>
#include <QProcess>
#include "qtservice.h"
class ubuntuService : public QObject,public QtService<QCoreApplication>
{
Q_OBJECT
public:
ubuntuService(int argc, ch

本文详细介绍了如何使用QtService在Ubuntu上实现后台服务,并结合systemd进行服务管理。通过QT创建服务,利用systemctl命令进行服务的启动、停止、重启和状态检查。此外,还讲解了systemd的unit配置文件和常用命令,以及如何创建和管理systemd服务单元。最后,展示了服务的测试效果和可能出现的问题。
最低0.47元/天 解锁文章
1986

被折叠的 条评论
为什么被折叠?



