Running Python code on startup using systemd. Code does start at boot up but stops and service ends

I have a Python code on NVIDIA Jetson TX@ (running Ubuntu 18.04) that records videos upon receiving commands (via serial communication with another hardware). The recording is done by the IP camera connected to the Ethernet Port of NVIDIA Jetson TX2. The video files are saved at the same path where the Python code is.

I would like to have the python code run on startup as soon as the board is booted.

The code stops running after the first serial command is sent. So if I want to send another command, it won’t work because the code isn’t running anymore upon checking ps -ax|grep python3. However, when I first boot Jetson, the process does show running. It just stops after the first serial communication.

My code is in /usr/bin/dummy_service.py .

Here is the systemd service configuration. I have named my service dummy.service:

> GNU nano 2.9.3                                                     /lib/systemd/system/dummy.service                                                                
[Unit]
Description=Dummy Service
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

If I check the status of the service after the code has stopped on it’s own after the first camera operation command, I get this:

sudo systemctl status dummy.service

● dummy.service - Dummy Service

   Loaded: loaded (/lib/systemd/system/dummy.service; enabled; vendor preset: enabled)

   Active: failed (Result: exit-code) since Thu 2020-11-12 19:42:44 IST; 22s ago

  Process: 7337 ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py (code=exited, status=1/FAILURE)

 Main PID: 7337 (code=exited, status=1/FAILURE)

Nov 12 19:41:57 jetson-desktop systemd[1]: Started Dummy Service.

Nov 12 19:42:44 jetson-desktop systemd[1]: dummy.service: Main process exited, code=exited, status=1/FAILURE

Nov 12 19:42:44 jetson-desktop systemd[1]: dummy.service: Failed with result 'exit-code'.

Any insights? I’m not sure what I’m doing wrong.

Thanks

Duplicate with:
https://forums.developer.nvidia.com/t/failing-to-have-a-python-code-run-on-startup-on-ubuntu-18-04-running-on-jetson-tx2-help/159351/4
(although this is centered more on using systemd, so this is probably the better URL to use…just don’t use two for the same thing)

I don’t have this to set up and try, but I see this as a conflict:

After=multi-user.target
...
WantedBy=multi-user.target

This is saying multi-user.target wants this, and that this is not allowed until multi-user.target is reached. You cannot both require this for multi-user.target and make this available until after multi-user.target.

You should also be very careful to find out if your software will run with no X/GUI session. Python would not have an issue with this, but some camera software must have an X context.