I have python program, I want to run it everyday with different variable (on CentOS server):

/usr/bin/python /root/testing/test.py --variables /root/testing/daily/test1.txt
while [ $? != 0 ]
    sleep 60
    /usr/bin/python /root/testing/test.py --variables /root/testing/daily/test1.txt

sleep 60 runs, but there is no python in htop and program output is empty. I changed python to /usr/bin/python because when I searched I found answer that it can fix. Also before running this python code i have another little one, which just updates old files, that program runs and works very. When I'm running this shell program from terminal entering sh tester.sh it works, problem is only when running from cron.

Also it worked in Ubuntu server. What is wrong? Can cron and shell script work different in CentOS?

Also, my crontab syntax:

22 23 * * * sh /root/testing/cron-work.sh >> /root/cron1.log
23 23 * * * sh /root/testing/cron-work2.sh >> /root/cron2.log
24 23 * * * sh /root/testing/cron-work3.sh >> /root/cron3.log
14 23 * * * sh /root/testing/cron-work4.sh >> /root/cron4.log
  • 2
    Try adding 2> /tmp/cron_log.txt (or whatever name you want) to the end of the python command line. That way, you can see if any errors are returned. – terdon Jan 15 '14 at 0:34
  • Sorry, missed to write cron syntax. Editing... – Emin Mastizada Jan 15 '14 at 0:36
  • Thanks but also try capturing errors as I suggested, that should give us some more info. – terdon Jan 15 '14 at 0:37
  • In ubuntu this cron commands writes everything from output of python program to log file. – Emin Mastizada Jan 15 '14 at 0:39
  • I think may be using without sh or with /bin/sh can work. Will test it when server will finish manually activated programs. – Emin Mastizada Jan 15 '14 at 0:41

try the following solution please:

chmod +x test.py
chmod +x /root/testing/cron-work.sh
chmod +x /root/testing/cron-work2.sh
chmod +x /root/testing/cron-work3.sh
chmod +x /root/testing/cron-work4.sh
echo "22 23 * * * sh /root/testing/cron-work.sh"  >> /root/cron1.log 
echo "23 23 * * * sh /root/testing/cron-work2.sh" >> /root/cron2.log
echo "24 23 * * * sh /root/testing/cron-work3.sh" >> /root/cron3.log
echo "14 23 * * * sh /root/testing/cron-work4.sh" >> /root/cron4.log

chmod +x root/cron1.log
chmod +x root/cron2.log 
chmod +x root/cron3.log 
chmod +x root/cron4.log 

/etc/init.d/crond start  #redhat based servers like centos
/etc/init.d/cron  start  #debian based servers like ubuntu

crontab cron1.log
crontab cron2.log
crontab cron3.log
crontab cron4.log
  • Already solved by moving programs to /usr/local/bin and now just wrting cron-work.sh or cron-work2.sh and etc. and it works. No need for writing path to programs. Thanks for your work too – Emin Mastizada Feb 10 '14 at 11:57
  • ok guy...you are welcome :) – MLSC Feb 10 '14 at 15:04
  • you can also upvote me ;) – MLSC Feb 10 '14 at 15:05
  • Sorry, I don't have 15 reputation in superuser, next time – Emin Mastizada Feb 11 '14 at 16:46

Nothing helped to run from path. Moved all programs to /usr/local/bin and added path for bash and for python to programs. Now I can run them just entering cron-work.sh in shell. And also in crontab writing program's name without path. Everything works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.