我找不到任何例子来说明如何在Linux中使用个性化的ManageSystemd服务来实现这种特殊需求。我需要在Ubuntu/Linux中创建一个函数,其中仅当我的服务在后台运行时,才会在系统托盘图标(桌面仪表盘托盘上的小图标)中显示图标。如果不是,则不会显示任何内容,这将帮助我了解在每次重新启动系统时,服务是否会自动正常运行(就像现在所做的那样).
我的系统服务文件位于/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_())
通过python进行轮询。每5秒钟检查一次“sudo systemctl status app.service”的输出中是否有关键字“active(running)”
下面的代码应添加到托盘图标的代码中。当服务未运行时,它将关闭托盘图标
相关问题 更多 >
编程相关推荐