Newbie question on service, python and console
I am new to both ubuntu and python scripting. I have written a python script which, when launched from the shell, monitors some events and writes those events to the shell. All that is working fine.
I would like the python script to run as a service on boot and used systemd to launch my .sh file. Now, after boot, if I check the status of my service, it says it is running but it is not writing to the shell.
I have googled around looking to see how to get the script's feedback visibly but to no avail. Can someone please point me in the right direction?
Thank you all. Happy holidays.
12 Answers
You must tell the system exactly which $DISPLAY you want the script to use.
Since there cannot be a $DISPLAY active at boot (before login), you seem to have a design problem.
Most folks have their script write to a file or to a log or to some other service, then retrieve the values using a different process that does have a $DISPLAY.
I didn't quite understand why you need the .sh file... If you need to run a Python script and get logs, this is usually done through a log file. I would do it like this:
Create a script in the home folder:
touch ~/example.py
nano ~/example.pyIn example.py:
from datetime import datetime
import time
while True: time.sleep(1) mytime = datetime.now().strftime("%m/%d/%Y, %H:%M:%S") with open("example.log", "a") as myfile: myfile.write(mytime + "\n")Next would create a service:
sudo chmod +x ~/example.py
sudo touch /etc/systemd/system/example.service
sudo nano /etc/systemd/system/example.serviceIn the service file:
[Unit]
Description=Example
[Service]
WorkingDirectory=/home/user/
ExecStart=/usr/bin/python3 /home/user/example.py
Restart=on-failure
[Install]
WantedBy=multi-user.targetReloading services:
sudo systemctl daemon-reloadThen you can use the service:
sudo systemctl start example
sudo systemctl enable example
cat ~/example.logSometimes you can use print(""). Then the service logs will be issued by the command: journalctl -u example -b
You can use my application to create services in Ubuntu, where this algorithm is abstracted from the user in favor of simplification: Linux Service
Happy Holidays to you too