CRONTAB - one python script is not running


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers CRONTAB - one python script is not running
# 1  
Old 05-27-2015
CRONTAB - one python script is not running

Hi!

I'm using a RaspberryPi with standard Raspbian.
Currently I'm working on some sort of weather station.

For now I have three python scripts - one which is updating txt files for website - update1m.py ( it will not be necessary in few next days so I'll delete it) , second one for updating mysql datebase with measurement - updateDB.py and the last ( this one is generating problem ) for generating chart out of measurements gathered in database.

I'm a beginner so propably my methods are as dumb as it is possible but this is the reason why I'm asking you for help.

Via command line im running - sudo crontab -e.
My crontab looks like this:

Code:
@reboot python /home/pi/meteo/update1m.py

@reboot python /home/pi/meteo/database/updateDB.py

@reboot python /home/pi/meteo/imag.py

*/1 * * * * python /home/pi/meteo/update1m.py

*/1 * * * * python /home/pi/meteo/database/updateDB.py

*/1 * * * * python /home/pi/meteo/imag.py

First two scripts are running properly after reboot and every minute.
Unfortunatelly last one imag.py is not running with cron. It is working properly if I simply execute it with python ./imag.py but not with cron.
I'm sure that path for script is ok.

Do you have any idea why it is acting like that?

Regards,
Mateusz
# 2  
Old 05-27-2015
Quote:
Originally Posted by bartocham
Do you have any idea why it is acting like that?
As it is, no, but i am astonished that even the other scripts work. cronjobs are usually started from "init" (process number 0) and the environment of init is usually empty, especially the variable PATH is not set. This means that "python" should not work, only "/some/path/to/python". Find out where the python executable is by calling:

Code:
which python

(depending on your shell you might replace "which" with "whence" or "whereis") and put the displayed path in the cron call, like this (analoguously for the other entries):

Code:
*/1 * * * * /some/path/to/python /home/pi/meteo/imag.py

For the same reasons there could be other environment contents which the script is missing when being called from cron. Enter "set" at the command prompt to display the whole environment of your shell to find out. You will have to set them in the script in this case.

I hope this helps.

bakunin
# 3  
Old 05-29-2015
Hi!

I dont't think that it was the problem cause in python scripts I have #!/usr/bin/python
at start.

Happily I found the bug - in my python script I had a part:

Code:
dlugosc_p = open("datebase_length.txt", "r")

- read database table length from external file. This file was located in home/pi/meteo so in the same place as imag.py script - but from cron the path for file couldn't be found. I changed this part of python script to -
Code:
dlugosc_p = open("/home/pi/meteo/datebase_length.txt", "r")

and now everything works fine.

Thanks for help Smilie
# 4  
Old 05-29-2015
Running a script as
Code:
python script.py

would override the line
Code:
#!/usr/bin/python

The question is how / why is python in the path at this point.

PS: */1 is superfluous. It's the same as *
# 5  
Old 05-29-2015
Cron may be setting a default PATH.

e.g. from Linux 2.6 man pages
Quote:
Several environment variables are set up automatically by the cron(8) daemon. SHELL is
set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's
owner. PATH is set to "/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by set-
tings in the crontab; LOGNAME is the user that the job is running from, and may not be
changed.
(I've no idea if that's the case on Raspberry though. Or on reboot, for that matter...)

Last edited by CarloM; 05-29-2015 at 08:59 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Running python script in cygwin

I have python installed here on windows: C:\Python27 and can run a script from the command line but would rather use cygwin. $ cd "C:\Users\cmccabe\Desktop\annovar" cmccabe@DTV-L2231M5J /cygdrive/c/Users/cmccabe/Desktop/annovar $ python run_batch_job.py Traceback (most recent call last):... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Solaris

Script problem when running on crontab

Hi guys! I created a backup script that works fine when I run manually, but when I put a crontab job to execute it the result are not the expected. (not a time problem). Here is my script: bash-3.00# cat /bk_tool/backup2.sh #!/usr/bin/csh clear set DIR_HOST='SCP08' ... (3 Replies)
Discussion started by: andredemartini
3 Replies

3. Shell Programming and Scripting

Delay in running script from crontab

I am facing an issue where sometimes crontab is running script with some delay. Below is the stmt in script and it is the only stmt in script. echo "running at `date` " >> CRONCHECK.log Below is the cron entry. 0 11 * * * CRONCHECK.sh Below is the time of run each day. running at Fri... (8 Replies)
Discussion started by: Nishant Singh
8 Replies

4. UNIX for Dummies Questions & Answers

crontab not running script

Hi All, I am having the below script to be run from crontab, it it doesnt run. 1 * * * * /home/cobr_ext/test.sh > /home/cobr_ext/temp.txt when i run i manally it runs without any issues. Could please help me as to why doesnt it run the script.:( (7 Replies)
Discussion started by: abhi_123
7 Replies

5. UNIX for Advanced & Expert Users

Particular script not running through crontab

Hi, I have created the below script, set -x # Set the Path of Environment file ENV_FILE_DIR=/opt/app/p1trp1c1/sybase/ecdwqdm/xrbid/QDM_Prod/bin LOG_DIR=/opt/app/p1trp1c1/sybase/ecdwqdm/xrbid/QDM_Prod/log export ENV_FILE_DIR export LOG_DIR # Set Audit Environment . ${ENV_FILE_DIR}/QDM.env... (8 Replies)
Discussion started by: yohasini
8 Replies

6. Shell Programming and Scripting

Crontab not running a script

Hi, I posted this in the Solaris forum but I think this one would be more appropriate. I created a script starting with the following lines: #!/usr/bin/ksh flag=n export flag typeset -i quant=0 (...) When running it I'm getting the following 2 errors: /tmp/tstscript/testfail.ksh:... (9 Replies)
Discussion started by: Cvg
9 Replies

7. Programming

Running programs from a Python script

Any idea how I can run a program from a phyton script? For example, in csh I will do $tpath/tsimplex base=$fbase data=$fdata restore=$frestore \ nxp=$nx nzp=$nz param=$param intp=$intp nlay=$nlay \ varp=$varp sigma0=$sigma maxiter=$maxiter tol=$tol \ ... (2 Replies)
Discussion started by: kristinu
2 Replies

8. Red Hat

Problems with script running with crontab

Hi, I'm trying the backup a few information commands of my Check Point FW (it's on a SPLAT linux machine). This is the script I wrote: #!/bin/bash cd /var/tmp/ file1=netstat_`/bin/date +%d%m%y`.txt file2=ifconfig_`/bin/date +%d%m%y`.txt file3=cpstatos_`/bin/date +%d%m%y`.txt... (2 Replies)
Discussion started by: nirsh
2 Replies

9. UNIX for Dummies Questions & Answers

Getting error when running script through crontab

Hi all, I wrote small script for Solaris and when I am running it through command prompt its ok, but when I trying to run it using crontab, i am getting error like: ld.so.1: dbloader: fatal: libACE.so: open failed: No such file or directory /tmp/file.sh: line 5: 8304 Killed ... (4 Replies)
Discussion started by: nypreH
4 Replies

10. UNIX for Dummies Questions & Answers

A command in a script not running in Crontab.

Hi, I made this script for TRU Unix 5.1 OS based Node. When the script was run manually all the commands were running properly. But when it was run from Crontab, one command is not running. This command is not running when the script is running from Crontab: #... (2 Replies)
Discussion started by: mystition
2 Replies
Login or Register to Ask a Question