【发布时间】:2021-06-21 00:53:41
【问题描述】:
我找不到任何示例,如何在 Linux 中使用个性化的管理 Systemd 服务来实现这些特殊需求。我需要在 Ubuntu/Linux 中创建一个功能,其中仅当我的服务 systemctl 它在后台运行时才在系统托盘图标(在桌面仪表板托盘小图标上)显示一个图标,如果没有,它将不显示任何内容,这将帮助我了解是否在每次重新启动系统时,服务都会自动正常运行(就像现在一样)。
我的系统服务文件正常位于/usr/lib/systemd/system/app.service,如果我要右键单击这个个性化图标,我需要有一个小弹出窗口,上面写着Kill the App Service 或Close the App Service 或只是Exit (这个命令会运行这个后台函数sudo systemctl stop app.service)。
我不是编码专家,但我刚刚开始,有人可以帮我解决如何编写此代码吗?
非常感谢
编辑 2-2:
@Deepak,这样看来是正确的:
while (True):
app = QApplication(sys.argv)
trayIcon = QSystemTrayIcon(QIcon('/home/USER/.app_example/app.png'), parent=app)
menu = QMenu()
exitAction = menu.addAction('Exit')
result = subprocess.run(['sudo', 'systemctl', 'status', 'app_example.service'],stdout=subprocess.PIPE).stdout.decode('utf-8')
if "active (running)" in result:
trayIcon.setToolTip('App Example')
trayIcon.show()
pass
else:
sys.exit()
time.sleep(5)
exitAction.triggered.connect(app.quit)
trayIcon.setContextMenu(menu)
sys.exit(app.exec_())
【问题讨论】: