1

以下のサービス ファイルを Ubuntu 16.04 デスクトップの場所 /lib/systemd/system/ に配置し、lightdm ディスプレイ マネージャーを使用して、python pillow ライブラリで画像を表示します。

[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 のジョブにしてみましたが、まったく機能しません。

lightdm GUI ユーザー セッションが終了する前にスクリプトによってイメージが表示されるように、サービスを lightdm サービスにする必要がありますかBind?Partof

シャットダウンまたは再起動が発生する前に、Python スクリプトを実行してユーザー ログイン画面内に画像を表示するサービス ファイルを作成する方法はありますか?

4

0 に答える 0