2

I have a python script which which calls bash script to get list of all new directories created in last hour. Python script is executable.

    #!/usr/bin/python
import subprocess
import os
import dicom
import time

dire = '.'
directories = subprocess.check_output(
        ['find', '/dicom', '-maxdepth', '1', '-type', 'd', '-mmin', '-120', '-type', 'd', '-mmin', '+5']
).splitlines()
number_of_directories = len(directories)
b_new = 0
for n in range(1,number_of_directories):
    dire_str = str(directories[n])
    #dire_str = str(dire_str)
    print(dire_str)
    for dirpath,dirnames,filenames in os.walk(dire_str,topdown=True):
        a =1
        for filename in filenames:
            print(dirpath)
            if filename[-4:] == '.dcm':
                firstfilename = os.path.join(dirpath, filename)
                dcm_info = dicom.read_file(firstfilename, force=True)
                if dcm_info[0x0019, 0x109c].value == 'epiRTme':
                    dirpath_nii = dirpath[:-4]
            a = dirpath[-3:]
            a = int(a)
            os.chdir(dirpath_nii)
                    subprocess.call('/home/sdcme/bin/nii_mdir_sdcme %s %s' % (a, a), shell=True)
                break
            break

Apparently if i call this python script standalone on command prompt, it works fine but when i set it to run after 60 min on crontab, the script throws following error:

Traceback (most recent call last):
  File "/home/sdcme/Final_concat_sdcme.py", line 9, in <module>
    ['find', '/dicom', '-maxdepth', '1', '-type', 'd', '-mmin', '-120', '-type', 'd', '-mmin', '+5']
  File "/usr/lib64/python2.7/subprocess.py", line 530, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib64/python2.7/subprocess.py", line 672, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1201, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
~    

Crontab Script

*/59 * * * * Final_concat_sdcme.py &>~concatenation.log

can some one point out what is the problem here.. Appreciate help a lot.

EDIT:

Cron output LOG Error:

niidicom_sdcme: Command not found.
niidicom_sdcme: Command not found.
niidicom_sdcme: Command not found.
niidicom_sdcme: Command not found.
niidicom_sdcme: Command not found.
niidicom_sdcme: Command not found.

Actually my python script calls nii_mdir_sdcme script calls niidicom_sdcme. When i run above script independently on terminal there is no error but when cron job cals the above script->nii_mdir_sdcme->niidicom_sdcme then there seems to be problem .

2
  • 1
    Don't overwrite the PATH variable - it's required to find system and user binaries. Instead, use the full path to the script and output file in the crontab. e.g. /home/sdcme/Findal_concat_sdcme Commented Aug 3, 2015 at 13:29
  • @arco444 I have edited my question with error log. can you please tell me what mistake i am making... Commented Aug 19, 2015 at 15:02

1 Answer 1

3

I'm afraid you're overriding $PATH with a path that doesn't contain the find command, don't you?

The following might work better:

directories = subprocess.check_output(
        ['/usr/bin/find', '/dicom', '-maxdepth', '1', '-type', 'd', '-mmin', '-120', '-type', 'd', '-mmin', '+5']
).splitlines()
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please review my question agaian as i have added error log too. ? many thanks
The fact you have overriden $PATH has an impact not only on commands you execute (such as find), but also on commands commands you execute execute (such as if nii_mdir_sdcme then executes niidicom_sdcme). So, you have to make sure any command executed, be it by you or by the other commands, is available in $PATH.
i changed my cron job script.. now instead of changing path i am giving path of the script like ~/script/nii_mdir_sdcme.. But still with these changes the error shown is same ..

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.