views:

99

answers:

2

Ubuntu Server 9.10, Here is my file, test.py

import commands
blkid = commands.getoutput('blkid')
print blkid

When I manually run (as SU) this:

python test.py

I get the output of the blkid as expected:

/dev/sda1: UUID="3f0ac5bb-f0da-4574-81f5-77844530b561" TYPE="ext4"
/dev/sda5: UUID="67df0e7c-74fb-47dd-8520-ad720fbed67d" TYPE="swap"
/dev/sdb1: UUID="85466892-8dae-461c-95da-b8f91c2e766b" TYPE="ext3"
/dev/sdc1: UUID="91b84635-21c2-4d9a-84f8-2bbaab16d41f" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdd1: UUID="6a52c830-0029-4154-80cb-f17274eb6fed" SEC_TYPE="ext2" TYPE="ext3"

However when I add this to my SU crontab:

* * * * * python /home/myusername/test.py > /home/myusername/output

The content of output becomes:

sh: blkid: not found

What am I missing here? Is the Python commands module only for certain SH-specific commands? I'm just try to run a system command and capture the output into a variable that I can parse.

+2  A: 

The problem is probably with your $PATH versus root's (os.environ['PATH'] if you're looking at it in Python rather than shell;-). root's PATH is typically very conservative (it would be risky for it NOT to be!) and since you're running blkid without specifying an absolute path that may easily mean that it's on your PATH but not root's.

So do which blkid as yourself at a shell prompt, sudo su or anyway become root, and echo $PATH -- you can rapidly verify the problem. Then you fix it by using blkid's absolute complete path in the getoutput call rather than just the bare identifier blkid as you're doing now.

Alex Martelli
That worked, thanks! I've always assumed the path when running a cronjob was the same as if you were actually typing in the shell.
Jakobud
A: 

The environment (and PATH) of a cron job is not necessarily the same as your login shell.

msw