Search code examples
pythonrsyncsystemd

sh: 1: rsync: not found


I made a python application and its running fine. So now i made a service still all fine. last part of my program was to sync some files to a different server, rsync command works when i run it as my own users. only when it runs with the server it doesnt work.:

import os
..
...
os.system("rsync --remove-source-files -p -e 'ssh -i ~/.ssh/id_rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa -o HostKeyAlgorithms=+ssh-rsa' -avq --chmod=a+rwx /source remoteuser@serviceip:/destination/")

When I check journalctl I can see this error:

 python3[361897]: sh: 1: rsync: not found

Here my service:

[Unit]
Description=Job Server Service
After=network.target

[Service]
WorkingDirectory=/home/myuser/project
Environment="PATH=/home/myuser/project/projectenv/bin"
ExecStart=/home/myuser/project/projectenv/bin/python3 /home/myuser/project/job_server.py
User=myuser
Group=myuser
Environment=PYTHONUNBUFFERED=1


[Install]
WantedBy=multi-user.target

First I thought its because the key file ~/.ssh/id_rsa is not accessable by the service but error message indicates it cant find rsync. is this because of the python virtual environment?

When I go into the virtual environment I can run the rsync without any issue. Can someone point me in the right direction?

source projectenv/bin/activate

Solution

  • In your service definition you set:

    Environment="PATH=/home/myuser/project/projectenv/bin"
    

    This makes the path only contain that directory. You probably want something more like:

    Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/myuser/project/projectenv/bin"
    

    i.e. something with paths to programs such as rsync.

    There's no real elegant way of appending to the path. various hacks I've seen would involve changing the execstart line to something like:

    bash -c "PATH=/home/myuser/project/projectenv/bin:$PATH exec python3 /home/myuser/project/job_server.py"
    

    instead of the line setting PATH explicitly.