我在带有 lightdm 显示管理器的 Ubuntu 16.04 桌面的 /lib/systemd/system/ 位置有以下服务文件,以使用 python 枕头库显示图像。
[Unit]
Description=Python script to show image on starting shutdown
DefaultDependencies=no
Requires=graphical.target
#Requires=lightdm.service graphical.target
Before= halt.target poweroff.target shutdown.target
[Service]
Environment="XAUTHORITY=/home/devops/.Xauthority"
Environment="DISPLAY=:0"
User=devops
Type=simple
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/usr/bin/python3 /home/devops/display_image.py
KillMode=none
TimeoutSec=300min
[Install]
WantedBy=graphical.target multi-user.target
下面给出了显示图像的python脚本
#!/usr/bin/python3
import time
from datetime import datetime
import psutil
from PIL import Image
start_time=datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
#log to check if the script is started
with open("/home/devops/pylogs.txt", "a+") as text_file:
print("Service script started on: {}".format(curr_time), file=text_file)
# open and show image
im = Image.open('/home/devops/shutdown_banner.jpg')
im.show()
# display image for 20 seconds before shutdown
time.sleep(20)
# hide image by killing the process
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
end_time=datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
##log to check if the script is exited
with open("/home/devops/pylogs.txt", "a+") as text_file:
print("Service exited on: {}".format(final_time), file=text_file)
该脚本根据添加到日志文件的开始和停止时间条目执行。但是,由于 gui lightdm 用户会话已被终止,因此不会显示图像。
如上所示,关闭停止作业该脚本正在由服务执行 20 秒,因为我包括在内time.sleep(20)
,但未显示图像。
由于 lightdm 是默认的显示管理器,我尝试让这个 python 脚本成为ExecStopPre
lightdm.service 的工作,但它根本不起作用。
我应该将我的服务设为 aBind
还是Partof
lightdm 服务,以便在 lightdm gui 用户会话终止之前由脚本显示图像?
有没有办法让我的服务文件执行 python 脚本在遇到关机或重启之前在用户登录屏幕中显示图像?