how do I make systemd service to run python project with conda env through .sh

Question:

I’m trying to run my code on startup through systemd
I want it to run using the current environment cause it’s a bit big and I don’t want to reinstall all of that

I have a .sh file that activates the python environment and calls the starting script on a big project
I then made a service that calls that sh

[Unit]
Description=service to start code 

[Service]
User=root
WorkingDirectory=/usr/bin
ExecStart=/home/administrator/Downloads/open_app/out_cam_app.sh



[Install]
WantedBy=multi-user.target

#https://transang.me/three-ways-to-create-a-startup-script-in-ubuntu/

but this fives these errors

22 18:17:56 smart-fk systemd[1]: Started service to start 
22 18:17:56 smart-fk my_bas_Script.sh[1417930]: /path/to/my_bas_Script.sh: line 3: /root/anaconda3/bin/activa>
22 18:17:56 smart-fk out_cam_app.sh[1417931]: /path/to/my_bas_Script.sh: line 6: python: command not found
22 18:17:56 smart-fk out_cam_app.sh[1417932]: /path/to/my_bas_Script.sh: line 7: conda: command not found
22 18:17:56 smart-fk systemd[1]: my_Service.service: Main process exited, code=exited, status=127/n/a
22 18:17:56 smart-fk systemd[1]: my_Service.service: Failed with result 'exit-code'.

my bash script looks like this in case it’s the problem (it works on its own tho)

#!/bin/bash
source ~/anaconda3/bin/activate env_name 
cd path/to/python/project
python python_start_point.py
conda deactivate

I also tried making the service call the code directly and that made the code work stackover flow post that I used for thatthen start downloading some files , which I can’t do on this machine
so it failed cause of the connection time out

What am I doing wrong here ?

Asked By: Mai

||

Answers:

Remove the WorkingDirectory=/usr/bin and add the executable path to:

Environment="PATH=/sbin:/bin:/usr/sbin:/usr/bin"

Answered By: Niraj Nandane
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.