Gawan
Posts: 14
Joined: Sat Mar 07, 2015 5:47 pm

FTP-Script does not work with Cronjob ? - Solved

Sun Dec 11, 2016 11:35 am

Hi,

my RapsberryPi should take a picture every 5 minutes (scheduled as cronjob) and send them to my FTP server.
I have put everything together in a sricpt

Code: Select all

sudo ntpdate de.pool.ntp.org
DATE=$(date +"%Y%m%d_%H%M")
/usr/bin/raspistill -st -n -t 2000 -o /home/pi/camera/$DATE.jpg -w 1024 -h 768 -q 100

HOST=ftp.meinserver05567.com
USER=dingdong
PASS=dingdong123

/usr/bin/ftp -pinv $HOST <<EOF
user $USER $PASS
cd /webcam/images1/
put $DATE.jpg
bye
EOF
Whenever I execute this script manually it works fine without any problems.

But when I execute it as cronjob, the FTP-Upload seems to stop in the middle without any reason and does nothing.

The FTP-Log looks like this:

Code: Select all

Connected to ftp.meinserver05567.com.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 8 of 400 allowed.
220-Local time is now 12:05. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
331 User dingdong OK. Password required
230-Your bandwidth usage is restricted
230 OK. Current directory is /
/etc/rc.localRemote system type is UNIX.
Using binary mode to transfer files.
250 OK. Current directory is /webcam/images1
local: 20161211_1205.jpg remote: 20161211_1205.jpg
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.
Unfortunately I have no idea how to find the error and how to fix it :(

lG
Gawan
Last edited by Gawan on Sun Dec 11, 2016 6:12 pm, edited 1 time in total.

Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 11:54 am

Not sure, but I expect it's a path problem. When cron runs something for you it does not have the same environment as you do when you are running in a terminal.

So, because you put the full path for the image here:

/usr/bin/raspistill -st -n -t 2000 -o /home/pi/camera/$DATE.jpg -w 1024 -h 768 -q 100

You should probably put the full path for the image here:
put /home/pi/camera/$DATE.jpg
My circuit's dead, there's something wrong.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 11:58 am

1) Could you post your crontab file? I know this is nitpicky, but I want to make sure we're talking about the same thing.

2) I think (just a guess) that that the problem is that the file is created in the "camera" subdirectory, but your FTP script doesn't take that into account. I might speculate further that when you test manually, you are in the 'camera' directory, so it (seems to) work.
If this post appears in the wrong forums category, my apologies.

User avatar
jojopi
Posts: 3290
Joined: Tue Oct 11, 2011 8:38 pm

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 12:27 pm

It is not necessary to write /usr/bin/ftp. cron's PATH may be minimal, but it will not be missing /usr/bin/.

You need to redirect stderr as well as stdout (2>&1), or you will miss messages such as "local: 20161211_1205.jpg: No such file or directory".

It is best to be in the correct directory locally, because "put /home/pi/camera/$DATE.jpg" will try to use the same path remotely too, and specifying a different remote path is ugly.

I am slightly intrigued to see where the spurious "/etc/rc.local" comes from.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 1:23 pm

jojopi wrote:It is not necessary to write /usr/bin/ftp. cron's PATH may be minimal, but it will not be missing /usr/bin/.
This!

It's amazing how widespread this "full path everything in cron" derangement
is.
You need to redirect stderr as well as stdout (2>&1), or you will miss messages such as "local: 20161211_1205.jpg: No such file or directory".
Or, better, setup some kind of MTA. Then you don't have to redirect
anything, and all the normal output will go somewhere that you can read it.

Note that I phrase it this way because there are two general ways to go
about this MTA setup:
1) Install some "normal" MTA (Dougie can supply the names of these) and
then it will come in as mail. The downside to this is that
configuring mail is never easy (despite what Dougie may say...)
2) Write your own, which just dumps the mail into a file somewhere (this
is what I do).
It is best to be in the correct directory locally, because "put /home/pi/camera/$DATE.jpg" will try to use the same path remotely too, and specifying a different remote path is ugly.
Yep. Use the "lcd" command in your FTP script. Or just "cd" in the shell
script before invoking FTP.
I am slightly intrigued to see where the spurious "/etc/rc.local" comes from.
I'm confused by this. Where did you see a referece to rc.local in the OP?
If this post appears in the wrong forums category, my apologies.

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

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 1:33 pm

Martin Frezman wrote:
I am slightly intrigued to see where the spurious "/etc/rc.local" comes from.
I'm confused by this. Where did you see a referece to rc.local in the OP?
I was surprised to see it in the output of the ftp session too.

Code: Select all

230 OK. Current directory is /
/etc/rc.localRemote system type is UNIX.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 1:52 pm

OK, yeah. Now I see it.
If this post appears in the wrong forums category, my apologies.

Gawan
Posts: 14
Joined: Sat Mar 07, 2015 5:47 pm

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:01 pm

@Major Tom

I did what you suggested, but now it says it does not find the requested file in the folder, althouth LS clearly shows that the file exists:

Code: Select all

230-Your bandwidth usage is restricted
230 OK. Current directory is /
Remote system type is UNIX.
Using binary mode to transfer files.
250 OK. Current directory is /webcam/images1
local: /home/pi/camera/20161211_1659.jpg remote: /home/pi/camera/20161211_1659.jpg
227 Entering Passive Mode (81,19,145,55,170,210)
553 Can't open that file: No such file or directory
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.
pi@RaspiCam:~/camera $ ls
20161211_1505.jpg  20161211_1530.jpg  20161211_1555.jpg  20161211_1620.jpg  20161211_1645.jpg  camera.sh
20161211_1520.jpg  20161211_1545.jpg  20161211_1610.jpg  20161211_1635.jpg  20161211_1659.jpg
pi@RaspiCam:~/camera $

Gawan
Posts: 14
Joined: Sat Mar 07, 2015 5:47 pm

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:02 pm

@Martin Frezman
This the crontab-entry

Code: Select all

sudo crontab -e
0-59/5 * * * *  /home/pi/camera/camera.sh >> /home/pi/camera/camera.log

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:05 pm

Tom's advice was bad for a number of reasons, but you should definitely re-read jojopi's comments. The "No such" error is referring to the non-existence of the needed *target* directories, not anything to do with the source file.
If this post appears in the wrong forums category, my apologies.

Gawan
Posts: 14
Joined: Sat Mar 07, 2015 5:47 pm

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:05 pm

@jojopi
"It is best to be in the correct directory locally,"
so you suggest to change to the /home/pi/camera path inside the Script before executing everything else ?
does the FTP-Command take the "local" path or do I have to switch path inside the FTP command as well ?

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:07 pm

Gawan wrote:@jojopi
"It is best to be in the correct directory locally,"
so you suggest to change to the /home/pi/camera path inside the Script before executing everything else ?
does the FTP-Command take the "local" path or do I have to switch path inside the FTP command as well ?
Either or, not both. Both won't hurt anything (will still work), but isn't necessary.

Easiest is to 'cd' in the shell script before running ftp - or you can use 'lcd' inside FTP.
If this post appears in the wrong forums category, my apologies.

Gawan
Posts: 14
Joined: Sat Mar 07, 2015 5:47 pm

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:07 pm

Martin Frezman wrote:Tom's advice was bad for a number of reasons, but you should definitely re-read jojopi's comments. The "No such" error is referring to the non-existence of the needed *target* directories, not anything to do with the source file.
Hm, I am afraid I dont get it.
The "target" directory is /webcam/images1/ on the ftp-server. I switch to this path before I do the PUT.
Should be fine, isn't it ?

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:13 pm

Gawan wrote:Hm, I am afraid I dont get it.
The "target" directory is /webcam/images1/ on the ftp-server. I switch to this path before I do the PUT.
Should be fine, isn't it ?
No.

From your most recent FTP log:

Code: Select all

local: /home/pi/camera/20161211_1659.jpg remote: /home/pi/camera/20161211_1659.jpg
The requested remote file name is

Code: Select all

remote: /home/pi/camera/20161211_1659.jpg
Does /home/pi/camera exist on your FTP server? I don't think so...
If this post appears in the wrong forums category, my apologies.

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

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 4:52 pm

Major Tom wrote:You should probably put the full path for the image here:
put /home/pi/camera/$DATE.jpg
This is wrong because it will try to send the file "/home/pi/camera/$DATE.jpg" to "/home/pi/camera/$DATE.jpg".

You shouldn't include path name in the file name to send.

Put this in your ftp directives

Code: Select all

lcd /home/pi/camera
cd /webcam/images1
put $DATE.jpg
That will set the local and remote directories to the correct values.

Gawan
Posts: 14
Joined: Sat Mar 07, 2015 5:47 pm

Re: FTP-Script does not work with Cronjob ?

Sun Dec 11, 2016 6:11 pm

Hi,

1. I added one line to the script before the FTP command --> cd /home/pi/camera
2. I removed the /usr/bin part before ftp

It was easy after I found out that I can test the script if I just go to a totally different folder and run it from there.

Thanks a lot for your support, problem solved ! :D

BR
Gawan

Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: FTP-Script does not work with Cronjob ?

Wed Dec 14, 2016 11:16 am

Martin Frezman wrote:Tom's advice was bad for a number of reasons, but you should definitely re-read jojopi's comments. The "No such" error is referring to the non-existence of the needed *target* directories, not anything to do with the source file.
My advice was not bad at all. I am curious to know the number of reasons why. I correctly identified that the cron environment did not start in the same directory. What I should have said was:
put /home/pi/camera/$DATE.jpg /webcam/images1/$DATE.jpg

And I disagree with rpdom that you shouldn't put a path in the filename to send. It just depends.

Anyway glad you fixed it. There are clearly several ways to do it.
My circuit's dead, there's something wrong.

Return to “Troubleshooting”