# How do I get this script to run periodically via cron?

I made a script that sends my local sensors temperature to a server in the internet and then I can see a graph of it. Everything works fine when I execute it manully. I have the latest ubuntu version. When it comes to run periodically every minute with crontab then nothing works. It doesnt send anything. I tried to run it as SUDO and as user, I tried to add it in the crontab file to run it from several locations like /bin/myscript.sh , /user/Desktop and many others...Nothing seems to work. Any Idea how to solve it? Thank you!

   echo Init ...
TEMP=/usr/bin/snmpwalk  -v 1 -c public 192.168.1.2 iso.3.6.1.4.1.38783.3.9.0 |  cut -d" " -f4
echo $TEMP REALTEMP=echo "scale=1;$TEMP/10"  | bc -l
echo $REALTEMP echo wget "http://api.thingspeak.com/update?key=MYKEY=$REALTEMP"

-
Welcome to AskUbuntu. As is, we have little information we could use to help you. Please consider adding the script file contents and the crontab file contents for us to be better able to locate the problem's source. –  NlightNFotis Sep 9 '12 at 16:15
ok I will do it write away. –  Vagelism Sep 9 '12 at 18:00
so.. where's the cron / crontab line you used? –  medigeek Sep 11 '12 at 11:32

I had a similar issue getting cron to recognize a command on a server of mine. The tip that solved my issue came from the troubleshooting section of the Ubuntu Cron Howto:

When adding a new entry to a blank crontab, forgetting to add a newline at the end is a common source for the job not running. If the last line in the crontab does not end with a newline, no errors will be reported at edit or runtime, but that line will never run. See man crontab for more information. This has already been suggested as a bug.

-
I did not know this, thanks for the answer! –  Michael Robinson Sep 14 '12 at 0:11

I don't have any clue about your script issue, but if your need is to have some realtime charts about the sensors' temperature available through a web server you can give a try to psensor-server.

It is available in the standard ubuntu repositories, so you can install it easily with 'sudo apt-get install psensor-server', run 'psensor-server', and finally open the URL 'http://hostname:3131'. You will get this kind of charts:

Alternatively, you can retrieve programmaticaly as a JSON output the temperatures using 'http://hostname:3131/api/1.0/sensors'.

-
Does this support snmp ? My I have a controller that sends back snmp responts that I trap and convert them to charts like this that you have in the screen shoot! –  Vagelism Sep 12 '12 at 12:39
No, it does not support SNMP. –  JeanFI Sep 13 '12 at 5:56
So is only for gathering computer statistics! Thank you anyway! –  Vagelism Sep 14 '12 at 9:26

Assuming your crontab line just contains the script name, you need specify what should execute the script, either in crontab (assuming it should run with bash shell)

/5 * * * * /bin/bash /path/to/myscript.sh


or by adding a shebang'' line at the beginning of the script

#!/bin/bash


It is not obvious that the script depends on any environment variables, but it is worth noting a cron job runs with only HOME, LOGNAME, PATH, and SHELL set.

The script file also needs to be executable (chmod +x myscript.sh).

-

Thank you all for your help. I manage to do it runing as a service with the help that I found in this site You can do what he said. Or, you can do this, which will run ANY sript for you.

1. First, create a script, and edit it to your needs. Lets say, you call it, autorun.sh
2. Copy that script into /etc/init.d
3. Run the following commands:

sudo update-rc.d autorun.sh defaults

sudo chmod +x /etc/init.d/autorun.sh

Again thank you all for your advices. I really didnt have time to check them all. I will do it soon!

-
Thank you for the editing! –  Vagelism Sep 16 '12 at 15:46