learnningprogramming
learnningprogramming

Reputation: 379

Python script running successfully on command prompt but not in crontab

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

Upvotes: 0

Views: 1212

Answers (2)

user3215375
user3215375

Reputation:

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

Upvotes: 0

dsh
dsh

Reputation: 12213

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.

Upvotes: 3

Related Questions