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.
The problem is probably with your
$PATHversus 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 runningblkidwithout specifying an absolute path that may easily mean that it’s on your PATH but not root’s.So do
which blkidas yourself at a shell prompt,sudo suor anyway become root, andecho $PATH— you can rapidly verify the problem. Then you fix it by using blkid’s absolute complete path in thegetoutputcall rather than just the bare identifierblkidas you’re doing now.