skip to Main Content

I have a python script that works fine when executed from command line. But when I try to run it from cron, it does not work.

command = "rsync -avHP --delete --bwlimit=800 rsync://mirror.nsc.liu.se/CentOS/7.8.2003/  /home/me/reposync/centos"
proc=subprocess.run(command.split(), capture_output=True)

The cronjob runs. The cronfile looks something like this:

PATH=/home/me
40 13 * * * me cd $PATH && ./reposync.py sync 2> /tmp/test.txt

But I get this error (yes, twice) from print(proc.stderr.decode('utf-8')):

-avHP: rsync: command not found

It seems like the problem has to do with not finding rsync but I don’t know what to do about it.

The output from /tmp/test.txt:

FileNotFoundError: [Errno 2] No such file or directory: 'rsync'

I have tried adding shell=True to the subprocess.run but it does not seem to make a difference. Or well it does not throw that exception, but I still get the same error when printing stderr from proc.

I suppose that I could solve it by including the absolute path to rsync but it feels like it’s a bad idea. But I could be wrong about that. If that is the right approach, please explain why.

2

Answers


  1. You might have to define shell in your crontab.
    The question has been asked previously.

    crontab: python script being run but does not execute OS Commands

    Login or Signup to reply.
  2. In your crontab, you are overwriting your $PATH variable, which now contains only the directory /home/me. Your rsync executable now cannot be found, since it is not in your home directory.

    Change your crontab entry by removing the PATH=... line and calling your script with it’s full path:

    40 13 * * * me /home/me/reposync.py sync 2> /tmp/test.txt
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search