0

I have written a python script.py in my ~home/ directory which calls other python scripts located ~home/bin. When i run script.py i am able to run it successfully but when i schedule script.py in crontab, script.py is not able to call script from bin directory.

Crontab script looks like this:

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

script.py calls another script as following:

subprocess.call('/home/sdcme/bin/nii_mdir_sdcme %s %s' % (a, a), shell=True)

can some one point it out why script.py is not able to call the other script. I suspect the problem is with PATh variable or other such thing but dont have nay idea how i should troubleshoot this..

Thanks!

Edit: nii_mdir_sdcme script calls another script niidicom_sdcme located in the same bin directory: Crontab mail, shows following error mail -

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

Summary:

cronatab-> script.py -> nii_mdir_sdcme -> niidicom_sdcme the problem is nii_mdir_sdcme is not able to call niidicom_sdcme. But when i run script.py independently on command prompt everything works fine..

nii_mdir_sdcme code:

#!/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

6
  • 1
    Are you certain that the cron job (script.py) is running? You may need to include the full path to that command in the crontab. Also, check the permissions of the second script. Is it executable, and is it accessible to the user that's running the cron job? Commented Sep 2, 2015 at 14:43
  • 1
    Did cron give a local email message about this? Check using mail or mutt. Commented Sep 2, 2015 at 14:44
  • 1
    @minopret i have edited my question with the mail error Commented Sep 2, 2015 at 15:22
  • Are you sure that niidicom_sdcme is not run? The error looks like printed by niidicom_sdcme itself i.e., it can't find run0$i or run$i commands (sh, bash, zsh produce different error messages here). Unrelated: &> looks like bash syntax (set SHELL explicitly in the crontab), subprocess.call uses /bin/sh if shell=True (you could use executable parameter to choose other shell), your script runs /bin/tcsh. Make sure there are no error due to difference in syntax between the shells. Commented Sep 4, 2015 at 9:31
  • @J.F.Sebastian if subporocess.call() is calling sh screipt while my script is written in tcsh, then the wrapper python script (nii_mdir_sdcme) shoudlnt have worked when run manually.. I know i am repeating but i am still not undertsnding the problem .. Commented Sep 4, 2015 at 13:54

2 Answers 2

3

It probably is a problem with $PATH. See crontab(5) regarding the environment cron uses to run jobs. One of the simplest solutions is to adjust your crontab entry to give the full path to the script:

59 * * * * /home/sdcme/bin/script.py &>~concatenation.log

Also check your email. cron will email you the output and any failures to run the job. If you don't have a mail server on your system you will want to install and configure one so that you get such notices.

PS. Using */59 as the minute spec is not very meaningful, so I changed it to the equivalent 59, above.

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

11 Comments

Thank you! i have edited my question with the error shown in mail. Does that help us to gain insight whats happening with the code.
Yes: your script nii_mdir_sdcme expects to find niidicom_sdcme in $PATH, but it is not. Maybe you want to add /home/sdcme/bin to $PATH in your crontab.
should i write $PATH = /home/sdcme/bin or PATH = /home/sdcme/bin.. Latter case is not working and i am afraid $PATH will change the original $PATH variable ..
@tryeverylanguage: you should use PATH=/home/sdcme/bin:/bin:/usr/bin (note: no $)
@tryeverylanguage: Do you use env call() parameter? What is print(os.environ['PATH'])? Do you see /home/sdcme/bin there? What is ls -l /home/sdcme/bin/nii_mdir_sdcme? Do you see executable permission for the user that runs crontab commands?
|
0

first of all use crontab -e to exit editor use :xi to save and exit then you have to edit your line from

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

to

*/59 * * * * /usr/bin/python script.py &>~concatenation.log

the /usr/bin/python is the path to your python

Hope that it works for you

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.