0

I'm running into a wall with regards to using subprocess.call in a python script running in a crontab. I have isolated this problem to be subprocess not able to find the 7z executable. I'm running this on FreeBSD 10.1, but that should not make a difference. I have tried adding PYTHONPATH=$PATH to crontab, I have tried adding shell=True to subprocess.call, and I have tried using /usr/loca/bin/7z rather than 7z. None of these have fixed the problem. The error that I get is the following:

/usr/local/bin/7z: realpath: not found
/usr/local/bin/7z: dirname: not found
exec: /../libexec/p7zip/7z: not found

Here is how I'm calling the script in crontab:

PATH=$PATH:/usr/local/bin
@every_minute           $HOME/test.py >> $HOME/test.error 2>&1

Here is the contents of my script (test.py):

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import subprocess
import tempfile

thing = 'blahblahblah'

errors = open('/home/myuser/error', 'wb')

with tempfile.TemporaryDirectory() as tmpdirname:
    tempthing = os.path.join(tmpdirname, thing)
    fh = open(tempthing, 'wb')
    fh.write(b'123')
    fh.close()
    zipname = '{}.zip'.format(thing)
    ziptempfile = os.path.join(tmpdirname, zipname)
    zipper = subprocess.call(['7z', 'a', '-p{}'.format('something'), '-tzip', '-y', ziptempfile, tempthing], stdout=errors, stderr=subprocess.STDOUT)
6
  • is 7z is /usr/local/bin? Commented Jun 10, 2015 at 20:26
  • Yes: $ whereis 7z 7z: /usr/local/bin/7z Commented Jun 10, 2015 at 21:27
  • you file is also executable? Commented Jun 10, 2015 at 21:28
  • -rwxr-xr-x 1 myuser myuser 439 Jun 10 21:19 test.py Commented Jun 10, 2015 at 21:29
  • -r-xr-xr-x 1 root wheel 112 Jun 4 18:38 /usr/local/bin/7z Commented Jun 10, 2015 at 21:29

1 Answer 1

2

The answer is that the PATH variable in crontab must use an absolute path like so:

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

That fixes everything.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.