3

I've got a problem with my cronjobs and I'm not able to solve it on my own.

That's what i wrote into crontab -e (with newline at the end):

SHELL=/bin/bash
*/1 * * * * /home/pi/Projekte/C/CRONTEMP.sh >/dev/null 2>&1

The file "CRONTEMP.sh" contains following:

#!/bin/bash
/home/pi/Projekte/C/Temperaturauslese
exit 0

This Script is working very well when I'm starting it in the terminal.

The File "Temperaturauslese" is a C programm, which I wrote to log any temperature changes in my area ... this is also working very well.

I'm trying to create a statistic about the temperature history in my house and I think without cron it would be much complex.

EDIT: The Problem is that cron is not working.

/var/log/syslog says:

Oct 14 17:37:01 raspberrypi /USR/SBIN/CRON[7203]: (root) CMD (/home/pi/Projekte/C/CRONTEMP.sh)
Oct 14 17:37:01 raspberrypi /USR/SBIN/CRON[7204]: (pi) CMD (/home/pi/Projekte/C/CRONTEMP.sh)
Oct 14 17:37:02 raspberrypi /USR/SBIN/CRON[7201]: (CRON) info (No MTA installed, discarding output)
Oct 14 17:37:02 raspberrypi /USR/SBIN/CRON[7202]: (CRON) info (No MTA installed, discarding output)

EDIT2: sourcecode of Temperaturauslese:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
FILE *Ausgabe, *Temp, *Date;
    Temp=fopen("Temp.txt","r");
    Ausgabe=fopen("Temperaturverlauf.txt", "a");
    Date=fopen("Date.txt","r");

system("python temperaturauslese.py > Temp.txt");
system("date > Date.txt");

double Temperatur=1.1;
char Datum[100];
fscanf(Temp, "%lf", &Temperatur);
/*fprintf(Ausgabe, "%lf\n", Temperatur);*/
fgets(Datum,  100, Date);
fprintf(Ausgabe, "%lf\t%s", Temperatur, Datum);

fclose(Temp);
fclose(Date);
fclose(Ausgabe);

return 0;
}

Sourcecode of temperaturauslese.py:

tfile = open("/sys/bus/w1/devices/10-000802b5cec1/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
print temperature

PS: THANK YOU FOR YOUR HELP - Solved the problem by using a bash script instead of python :-)

19
  • You haven't mentioned any problems. If it fails to run look in /var/log/syslog for error messages. Is this a root crontab? If it is a user crontab perhaps the permissions are wrong.
    – joan
    Commented Oct 14, 2014 at 15:30
  • I wrote the Cron problems from syslog in the Question :-) Sry thought "cron not working" is a problem... ; no its no root crontab .. thats why i wrote "crontab -e" Commented Oct 14, 2014 at 15:40
  • I don't think that the crontab should contain a line SHELL=/bin/bash
    – Ghanima
    Commented Oct 14, 2014 at 15:56
  • 1
    Normally, if a cron entry produces output on stdout or stderr, that output is mailed to the owner of the crontab. What output does Temperaturauslese produce? What do you want cron to do with that output? Commented Oct 14, 2014 at 16:35
  • 1
    Try changing the cron entry to */1 * * * * /home/pi/Projekte/C/CRONTEMP.sh >/dev/null 2>&1 Commented Oct 14, 2014 at 17:10

2 Answers 2

2

Perhaps you can start by setting your bash script to do a simpler task - like echo "hello i ran" into a file somewhere accessible, world writeable, every minute.

*/1 * * * * (echo "hello world" >>/var/log/it_ran.txt)

Then come back in a minute to see if this actually ran. You can then tell cron did something.

The thing about cron, versus manually starting your scripts and code is that it will have a different environment, so things you may have assumed about env, working directories and paths may be wrong.

For example - you make a couple of system calls, I'd not guarantee that python is in your path at that point, so you may need /bin/env python <script and params> instead.

One helpful thing maybe to have your shell script print the environment and redirect into a known location to see what it got, run it manually and then let cron run it.


Looking at the additional stuff in your question, try redirecting your run output to somewhere instead of dev nulling it, or letting cron try and mail it to admin (its default behaviour). Then you may better be able to see what is going on.

5
  • Ok thank you, I'll do that tomorrow :-) do you mean I have to put /bin/env python into the crontab? Commented Oct 15, 2014 at 23:03
  • That may be a start - watch out for other assumptions about path and environment. Commented Oct 16, 2014 at 19:19
  • specifically: */1 * * * * echo "hello world" Commented Oct 17, 2014 at 20:16
  • adding */1 * * * * echo "hello world" changes nothing .. Commented Nov 28, 2014 at 20:06
  • The hello world was simply an example. The redirect of the output is the important point. This is done by >> followed by the output file name. Commented Nov 30, 2014 at 0:52
1

if their is a delay in the python script() in returning the temperature and the cron is set to run at intervals.

You can use a lockfile function or a simple presence of a file in a directory to prevent the second (subsequent) cron jobs running until the first is ready.

simple example

Create directory 'test'

#!/bin/bash
FILE=""
DIR="test"
# init
# look for empty dir 
if [ "$(ls -A $DIR)" ]; then
     echo "Take action $DIR is not Empty"
else
    echo "$DIR is Empty"
fi
# rest of the logic

save above code in anything.sh

then chmod +x anything.sh the above script file

./anything.sh to execute

I would try either the lockfile or the above simple create file delete file, I have experienced where the lockfile function fails and the cron runs regardless so I then created the test to check if a file exists which is created when the cron starts and deleted when it finished.

1
  • thanks for the answer. i think that maybe the python script slower than anything else. my "work around" works very well for my, but i'll follow your advice. Commented Mar 12, 2015 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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