-1

I am calling a tcsh script in my python program. The tcsh script takes 10-12 mins for completion. But as i call this script from python, python interrupts script before it executes completely. here is the code snippet.

import subprocess
import os
os.chdir(dir_path_forCD)
subprocess.call('/home/sdcme/bin/nii_mdir_sdcme %s %s' % (a, a), shell=True)
print(a+1);

Can some one point out, how i can call nii_mdir_sdcmescript from python without interrupting(killing) script before it is executed completely.

The complete script is as follows:

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

dire = '.'
directories = subprocess.check_output(
    ['find', '/Users/sdb99/Desktop/dicom', '-maxdepth', '1', '-type', 'd', '-mmin', '-660', '-type', 'd', '-mmin', '+5']
).splitlines()
number_of_directories = len(directories)
b_new = '.'
for n in range(1,number_of_directories):
    dire_str = (directories[n])
    dire_str = str(dire_str)    #[2:-1]
    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)
                dir_path_forCD= dirpath
                dcm_info = dicom.read_file(firstfilename, force=True)
                if dcm_info[0x0019, 0x109c].value == 'epiRTme':
                    os.chdir(dir_path_forCD)
                    subprocess.call('/home/sdcme/bin/nii_mdir_sdcme %s %s' % (a, a), shell=True)
                    print(a+1);

                break
             break
    break

tcsh script: nii_mdir_sdcme

#!/bin/tcsh
if ($#argv < 2) then
  echo "Usage: nii_mdir_sdcme start_dir# end_dir#"
  exit
else    
 set start = $argv[1]
 set end = $argv[2]

  if ( ! -d ./medata ) then
   sudo mkdir ./medata
 endif
 sudo chown sdcme ./medata
 sudo chgrp users ./medata

 set i = $start
 while ( $i <= $end )
   echo " "
   if ( $i < 10 ) then
     echo "Entering 000$i..."
     cd 000$i
     sudo chmod 777 .
     niidicom_sdcme run0$i
     #mv *+orig.* ../medata
     sudo chmod 755 .
   else
     echo "Entering 00$i..."
     cd 00$i
     sudo chmod 777 .
     niidicom_sdcme run$i
     #mv *+orig.* ../medata
     sudo chmod 755 .
   endif

   cd ..

   @ i++  
 end

endif
4ae1e1
  • 7,228
  • 8
  • 44
  • 77
  • Python is not interrupting the script. Something else is. We'll need more information before we can begin to diagnose this. – Kevin Mar 22 '15 at 19:51
  • @Kevin edited my question with complete script. – learnningprogramming Mar 22 '15 at 20:05
  • 1
    `subprocess.call` "run the command described by args; wait for command to complete, then return the returncode attribute." `subprocess.call` blocks and doesn't kill your program. What's wrong happens on bash script's side, not the python side. – 4ae1e1 Mar 22 '15 at 20:21
  • @4ae1e1 actually the bash scripts prints lot of output on terminal if i run it manually. Is this the reason that when i call the script from python, bash script cannot print it and hence process terminates before completion. – learnningprogramming Mar 22 '15 at 20:36
  • @tryeverylanguage Please include the bash script. There's little I can say without looking at the script. – 4ae1e1 Mar 22 '15 at 20:38
  • @4ae1e1 thanks. made suggested edit – learnningprogramming Mar 22 '15 at 20:41
  • Okay, this is not even bash. This is `tcsh`. I'm not an expert on `tcsh`, but I'd say you don't have the necessary permissions, given that `sudo` is all over the place in the script. – 4ae1e1 Mar 22 '15 at 20:42
  • You can try to run the python script as root to see what happens (`sudo python SCRIPT_NAME`). – 4ae1e1 Mar 22 '15 at 20:46
  • @4ae1e1 apparently it gives the same error. Just that you know both the script has same access level. -rwxr-xr-x – learnningprogramming Mar 22 '15 at 21:01
  • It has nothing to do with the access level, because you explicitly called `sudo` in the script. You can also try to switch to root by `sudo su` and try it again. Another problem: I don't know what `niidicom_sdcme` is. I suppose it's another script. Suggestion: include in the question what you get when you run the script in the interactive shell, and what you get when you run it through python. That usually gives a lot more information. – 4ae1e1 Mar 22 '15 at 21:18
  • 3
    By the way, one minor point: I would never do `chmod 777`. Especially not `sudo chmod 777`, which means you need root privilege to even modify the mode bits. That's asking for trouble. – 4ae1e1 Mar 22 '15 at 21:24
  • @4ae1e1: Well, `/tmp` has mode `1777`, and that's *almost* the same... – Kevin Mar 23 '15 at 13:13
  • @Kevin I don't see a use case where you need to create a /tmp yourself. – 4ae1e1 Mar 23 '15 at 15:16
  • @4ae1e1: `~/public_directory` or something of that nature. – Kevin Mar 23 '15 at 18:30
  • @Kevin No. The OP changed the mode back to 755 later. Also, other than `/tmp` I can't see why you want a directory to be world-writable. – 4ae1e1 Mar 23 '15 at 18:33
  • Also, you don't need root permissions to `chmod 777` for `~/public_directory`. Unless you are root, in which case you also don't need `sudo`. – 4ae1e1 Mar 23 '15 at 18:35
  • @4ae1e1: *shrug*. God only knows what horrible monstrosities live under `/opt` and such... Or `/mnt`, for that matter (assuming you've got something mounted there). – Kevin Mar 23 '15 at 21:26
  • Thansks all! Got a solution to it... – learnningprogramming Mar 24 '15 at 13:28

1 Answers1

0

The problem was with the counter a, which i am passing to call tcsh script.

Now it seems that, the problem was never with the python interrupting tcsh script. subprocess lets tcsh script run without interruptions with shell = True.