hmojal
Posts: 32
Joined: Thu Jul 24, 2014 1:39 pm

Problem with crontab

Mon Aug 25, 2014 1:29 pm

Hi all,

I have a script which takes a picture with the raspberry cam and then upload it to a dropbox.
The script is running succesfully when i just tip

Code: Select all

./testcam.sh
The thing is I need this script to run every 2 minutes.
So I looked and find that linux as the crontab function which is made for that.
I tip "sudo crontab -e" and updated this file this way :

Code: Select all

# Edit this file to introduce tasks to be run by cron.
*/2 * * * * pi /home/pi/testcam.sh
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
and so on..

And... nothing happen every two minutes....
On the link of the dropbox uploader script (https://github.com/andreafabrizi/Dropbox-Uploader) there are things sayed about the use with a cron job but I don't get it..

Any one for a big help ?

Thank's !

Hugo

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Problem with crontab

Mon Aug 25, 2014 1:39 pm

The thing is I need this script to run every 2 minutes.
So I looked and find that linux has the crontab function which isn't made for that.
Here we go again.

Read my comments in the other "crontab" thread.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

hmojal
Posts: 32
Joined: Thu Jul 24, 2014 1:39 pm

Re: Problem with crontab

Mon Aug 25, 2014 2:00 pm

You posts telling not to use crontab ?

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Problem with crontab

Mon Aug 25, 2014 2:02 pm

Joe Schmoe wrote:Here we go again.

Read my comments in the other "crontab" thread.
You meant this one http://www.raspberrypi.org/forums/viewt ... 08#p602708 where you say
Joe Schmoe wrote:This thread bears out well my constant assertion that cron is complex and weird and not the right tool for most things on the Pi. The fact that "mail" doesn't work (even locally) is a joke and renders most cron debugging jobs into the "what should be a 5 minute debugging job becomes a 6 month one" category. (Credit marty@winbatch.com for this last nice turn of phrase…)

It is especially not the right tool for running something "every minute", for reasons I've detailed at length in other posts - there are clearly better ways to "run something every minute".
So we have to look somewhere else again (after googling for it?) what these better ways, according to you, are?

tberry
Posts: 5
Joined: Mon Sep 08, 2014 7:11 pm

Re: Problem with crontab

Mon Sep 08, 2014 7:18 pm

I have precisely the same problem as the OP and saw Joe Schmoe's comments in the thread linked earlier. I searched for his comments on how to run a script every minute without using a cron job but could not find them. @Joe Schmoe, could you link me to them , please?

As I mentioned, I have the exact same problem. I am taking a picture from a webcam every minute. I need to ftp it to a server every minute. I have written a very simple script using wput which does this when I run ./simple.sh in the terminal. It will not run when I try to schedule it as a cron job, even though the system log shows that is is running each minute. It simply fails to run.

I don't want to hijack this thread with my details but let's just say I've tried a LOT of things including using cron.d. I suspect my solution is the same as the OP's and if Joe Schmoe has suggestions that work, I'm all ears.

tberry
Posts: 5
Joined: Mon Sep 08, 2014 7:11 pm

Re: Problem with crontab

Mon Sep 08, 2014 8:11 pm

I may have an answer and a solution. I Googled why it was a bad idea to run a cron job every minute and it appears that if your job doesn't finish in the one minute you allow, it will run the job again on top of itself. Cron apparently does not keep this from happening.

Suggested solutions were to simply code an infinite loop script with the command, tell it to sleep a few seconds after running, then loop. I am using wput to upload the image webcam.jpg from a folder on the desktop called webcam via ftp and will try a script that looks like this:

Code: Select all

#!/bin/bash
while true
do
wput --binary --user=your_ftp_username --password='your_ftp_password' ftp://ftp.your_ftp_location_.com /home/pi/Desktop/webcam/webcam.jpg
sleep 60
done
To ensure this file runs on reboot (say, from a power outage), follow the instructions here: http://www.raspberrypi.org/documentatio ... c-local.md Pay close attention to the advice to include an ampersand (&) at the end of the command or the script will never finish and prevent full boot of the device.

I'll try this and report back.

User avatar
DougieLawson
Posts: 39126
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Problem with crontab

Mon Sep 08, 2014 8:13 pm

cron doesn't run with your regular environment, it has a restricted PATH. So the usual solution to "I run foobar with cron and it doesn't work" is to ensure that you run /home/pi/foobar (fully qualified name) and every file it reads or writes is referenced as /home/pi/filetoread or /home/pi/filetowrite rather than ~/filetoread or ~/filetowrite or even filetoread or filetowrite.

You can also stick a bunch of echo or print statements in your script and when it's run by cron that stuff written to stdout gets sent to your id by email (assuming you have something like exim4 configured as a MTA).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Problem with crontab

Mon Sep 08, 2014 8:35 pm

Going back to the original issue
hmojal wrote:The thing is I need this script to run every 2 minutes.
So I looked and find that linux as the crontab function which is made for that.
I tip "sudo crontab -e" and updated this file this way :

Code: Select all

# Edit this file to introduce tasks to be run by cron.
*/2 * * * * pi /home/pi/testcam.sh
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
Apart from the fact that you should add new lines after all those messages (that won't stop it working, it just looks silly), you've used the wrong crontab format.
The /etc/crontab file uses the fields:

Code: Select all

Minute Hour DayOfMonth Month DayOfWeek Userid Command
whilst the user crontabs (that you get with the crontab command) use

Code: Select all

Minute Hour DayOfMonth Month DayOfWeek Command
Note the lack of Userid?

Your crontab entry is trying to run the command "pi" every two minutes, with the parameter "/home/pi/testcam.sh".

What's more, by using "sudo" you are editing the root crontab, not the pi crontab.

What you should do is use sudo crontab -e again and delete that line, then use crontab -e (without sudo) and add the following line to the end of the file

Code: Select all

*/2 * * * * /home/pi/testcam.sh

User avatar
socialdefect
Posts: 110
Joined: Mon Jun 25, 2012 9:02 pm
Location: Tilburg, the Netherlands
Contact: Website

Re: Problem with crontab

Mon Sep 08, 2014 11:25 pm

To prevent multiple instances from being started you can add something like this to your script:

Code: Select all

Pid=`ps aux | grep $0 | awk '{ print $2 }'`

YourScript(){
  -- paste the script here ----
}

if [ -x $Pid ] ; then
    YourScript
else
   while kill -0 "$Pid"; do
            sleep 0.5
   done
    YourScript
fi
This is more failproof than running sleep. This won't prevent multiple instances from being started if the sync task takes more than 2 minutes but it will prevent multiple sync instances from running at the same time.
== If it's not broke... I'm not done fixing it! ==

tberry
Posts: 5
Joined: Mon Sep 08, 2014 7:11 pm

Re: Problem with crontab

Tue Sep 09, 2014 2:06 pm

The script solution I posted above worked.

Thanks to rpdom for explaining the userid in cron issue. I had not noticed the difference and that is likely what was wrong with my job.

I will also include socialdefect's code to check and kill the process rather than sleep.

I have a different issue now with wput uploading only part of the jpg but will post in a separate thread on that issue if I can't find a solution.

Thanks for the help!

Return to “Beginners”