swampdog
Posts: 238
Joined: Fri Dec 04, 2015 11:22 am

Re: rsync - works from terminal but not as cron???

Mon Jul 22, 2019 9:56 pm

This all seems to be getting very complicated!

First off, put what you want to do in a script. eg: a normal cronjob on one of my pi's..

Code: Select all

admin@pi05:~ $ crontab -l
PATH=/usr/local/sd/bin:/$HOME/bin:/bin:/usr/bin:$PATH
#------------------------------------------------------------------------------
#min	hrs	dom	mon	dow	/path/to/cj
#------------------------------------------------------------------------------
02	08	*	*	*	cjx dfh
#------------------------------------------------------------------------------
##04	08	*	*	fri	cjx cj-yum
##05	08	1	*	*	cjx cj-tune2fs
#------------------------------------------------------------------------------
The environment for 'cron' is different from the command line so typically override it like I have above or put full path to your script in there (and likely override PATH at the beginning of your script). If you don't use 'vi' then this will allow you to edit the current user crontab with nano..

Code: Select all

admin@pi05:~ $ EDITOR="nano -w" crontab -e
..or for root..

Code: Select all

admin@pi05:~ $ sudo env EDITOR="nano -w" crontab -e
..and simple..

Code: Select all

admin@pi05:~ $ sudo crontab -l
PATH=/usr/local/sd/bin:/$HOME/bin:/bin:/usr/bin:/sbin:/usr/sbin:$PATH
#------------------------------------------------------------------------------
#min    hrs     dom     mon     dow     /path/to/cj
#------------------------------------------------------------------------------
01	03	*	*	*	cjx cj-updatedb
#------------------------------------------------------------------------------
..to view root crontab. I'd advise against root cron jobs, if possible though, for sanity.

So, create a script "foo" as yourself:

Code: Select all

$ cd
$ mkdir -p bin
$ touch bin/foo
$ chmod u+x bin/foo
$ echo 'sync -a -e ssh /home/pi/Public/ pj@10.1.1.155:/home/pj/Public' > bin/foo
$ ./bin/foo
Actually methinks what you want in "foo" is this initially..

Code: Select all

rsync --progress -auv /home/pi/Public/ pj@10.1.1.155:/home/pj/Public/
^^^note the trailing forward slash on both folders. I suspect this is what you actually want.

You need to get the public key from your machine onto the "pj" machine. It's not clear what you've done here. Typically..

Code: Select all

$ ssh-keygen -t rsa
^^^just hit enter for the prompts. I'm assuming "rsa" but it can be another type. We'll stick with "rsa" here. On the "pj" machine do the same thing if you've not already done so and a bit more to ensure that the 'sshd' is working on the ubuntu "pj" box..

Code: Select all

pj$ ssh-keygen -t rsa
pj$ cd .ssh
pj$ cat id_rsa.pub >> authorized_keys
pj$ chmod 600 authorized_keys
Check "pj" can log into itself..

Code: Select all

pj$ ssh pj@localhost
^^^if this fails, fix it before proceeding (is 'sshd' running?). I can't recall if ubuntu 19.04 activates 'sshd' by default. If it works just ^D (or type "exit") to log back out of yourself - back to yourself.

On the original, copy the key across..

Code: Select all

$ scp .ssh/id_rsa.pub pj@10.1.1.155:~.ssh/z
On "pj" add it to authorized_keys..

Code: Select all

pj$ cd
pj$ cd .ssh
pj$ cat z >>authorized_keys
pj$ rm -v z
^^^note the 'cat' used two ">" (ie ">>") to append not overwrite!

At this point you can log in..

Code: Select all

$ ssh pj@10.1.1.155
pj$ hostname
pj$ ^D
$ hostname
If the IP addresses are changing you may have to do some more fiddling to circumvent "man in the middle" warnings. That's for another day though.

Quick summary to setting up ssh manually so the above makes more sense:
  • Make sure 'sshd' is running.
    Generate key on target then append public part (.pub) to authorized_keys.
    Check permissions of authorized_keys is 0600.
    Have target log into itself without a password.

    Generate key on source machine.
    Copy public part to target, making sure to not overwrite(*) the target's public key.
    Append copied public key to target's authorized_keys.
    Test connection from source to target.

(*) which is why I named it "z" above else that's exactly what'll happen.

dunbrokin
Posts: 29
Joined: Sat Jan 05, 2019 3:16 am

Re: rsync - works from terminal but not as cron???

Tue Jul 23, 2019 1:34 am

Actually, I did not have to do all that....I just checked the folder and found that it is working.....I had to reboot the Pi a few days ago...and that seems to have done the trick!....Thank you all for your help, much appreciated.

dunbrokin
Posts: 29
Joined: Sat Jan 05, 2019 3:16 am

Re: rsync - works from terminal but not as cron???

Wed Aug 07, 2019 5:32 am

Actually, it seems that it only works intermittently....and fails after a few days! Not sure why ....so have to try the way swampdog suggests...

I have been working my way through swampdog's suggestions and have hit a problem with...
scp .ssh/id_rsa.pub pj@10.1.1.155:~.ssh/z
I get the response....
pi@WeatherPi:~ $ scp .ssh/id_rsa.pub pj@10.1.1.155:~.ssh/z
scp: ~.ssh/z: No such file or directory
What have I done wrong?

User avatar
DougieLawson
Posts: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: rsync - works from terminal but not as cron???

Wed Aug 07, 2019 7:09 am

dunbrokin wrote:
Wed Aug 07, 2019 5:32 am

Code: Select all

pi@WeatherPi:~ $ scp .ssh/id_rsa.pub pj@10.1.1.155:~.ssh/z
scp: ~.ssh/z: No such file or directory
What have I done wrong?
You missed a slash.

scp .ssh/id_rsa.pub pj@10.1.1.155:~/.ssh/z
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

dunbrokin
Posts: 29
Joined: Sat Jan 05, 2019 3:16 am

Re: rsync - works from terminal but not as cron???

Wed Aug 07, 2019 7:54 am

Thank you that missed / did the trick......
pi@WeatherPi:~ $ scp .ssh/id_rsa.pub pj@10.1.1.155:~/.ssh/zid_rsa.pub 100% 394 151.2KB/s 00:00
pi@WeatherPi:~ $
However I still get this on the main pc (pj) when I follow the instructions:
pj@pj-selgbuild:~/.ssh$ cat id_rsa.pub >> authorized_keys
pj@pj-selgbuild:~/.ssh$ chmod 600 authorized_keys
pj@pj-selgbuild:~/.ssh$ ssh pj@localhost
identity_sign: private key /home/pj/.ssh/id_rsa contents do not match public
pj@localhost's password:
:

I am not sure why I am making this so complicated (lack of knowledge on my part I guess!)...it really should be an easy task!

swampdog
Posts: 238
Joined: Fri Dec 04, 2015 11:22 am

Re: rsync - works from terminal but not as cron???

Wed Aug 07, 2019 4:16 pm

My bad. As DougieLawson says, I missed a slash..

Code: Select all

$ scp .ssh/id_rsa.pub pj@10.1.1.155:~/.ssh/z
identity_sign: private key /home/pj/.ssh/id_rsa contents do not match public
I suspect you've overwritten one keypair with part of the other. FYI.. there's a private key (id_rsa) and a public key (id_rsa.pub) and together they form a keypair. Each machine generates a keypair when you 'ssh-keygen'. In order to have machine A connect to machine B without a password you need to get Machine A "id_rsa.pub" into "authorized_keys" of Machine B. To prevent the initial "are you sure you want to connect?" prompt you also need to get Machine A "id_rsa.pub" into "known_hosts" of Machine B - but this is done for you on the initial connect from A into B and you won't see that prompt again unless "ssh" detects a problem.

Assuming there's no other keys on either machine then the easiest way for you to fix this is to delete the ".ssh" folders on both machines and redo it from scratch. Take care not to copy "id_rsa.pub" from one machine over the "id_rsa.pub" of the other machine. Note also: in this scenario there is never a need to copy the private key (id_rsa) anywhere - ever.

dunbrokin
Posts: 29
Joined: Sat Jan 05, 2019 3:16 am

Re: rsync - works from terminal but not as cron???

Thu Aug 08, 2019 8:06 am

Something had gone weird!
pi@WeatherPi:~ $ crontab -l
pi@WeatherPi:~ $ cd
pi@WeatherPi:~ $ mkdir -p bin
pi@WeatherPi:~ $ touch bin/wdataxfer
pi@WeatherPi:~ $ chmod u+x bin/wdataxfer
pi@WeatherPi:~ $ echo 'sync -a -e ssh /home/pi/Public/ pj@10.1.1.155:/home/pj/Public' > bin/wdataxfer
pi@WeatherPi:~ $ ./bin/wdataxfer
sync: invalid option -- 'a'
Try 'sync --help' for more information.
pi@WeatherPi:~ $ ./bin/wdataxfer
sync: invalid option -- 'a'
Try 'sync --help' for more information.
pi@WeatherPi:~ $

tpyo kingg
Posts: 615
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: rsync - works from terminal but not as cron???

Thu Aug 08, 2019 8:09 am

sync is a different program from rsync. You want the latter in this case.

See the manual pages for comparison: "man sync" versus "man rsync"

dunbrokin
Posts: 29
Joined: Sat Jan 05, 2019 3:16 am

Re: rsync - works from terminal but not as cron???

Thu Aug 08, 2019 9:01 am

sync is a different program from rsync. You want the latter in this case.
Ah! of course...thank you for reminding me!

Return to “Troubleshooting”