1

I have the below service file in the location /lib/systemd/system/ of Ubuntu 16.04 desktop with lightdm display manager to display an image with python pillow library.

[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

And the python script to display the image is given below

#!/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)

The script is executed as per the start and stop time entries that are added to the log file. However the image is not displayed as the gui lightdm user session is killed already.

Script execution as stop job

As shown above shutdown stop job the script is being executed by the service for 20 seconds as I included time.sleep(20) but the image is not displayed.

Since lightdm is the default display manager, I have tried are making this python script an ExecStopPre job of lightdm.service which doesn't work at all.

Should I make my service a Bind or Partof to the lightdm service so that the image is displayed by the script before the lightdm gui user session is terminated?

Is there a way to make my service file that executes the python script to display an image within user login screen before shutdown or reboot is encountered?

Yugendran
  • 172
  • 2
  • 9

0 Answers0