My goal is to transfer to a distant raspberry pi the stream of snapshots acquired through 'motion' software. I am using 'sftp' in the following test script:
#!/bin/bash cp $1 /tmp/snapshot-copy.jpg sudo -u motion /bin/bash << EOF cp $1 /tmp/snapshot-copy.jpg EOF export SSHPASS=MyServerPassword sshpass -e sftp -oBatchMode=no -oPort=9999 -b - pi@MyServerIPAddress << ! put /tmp/snapshot-copy.jpg bye !
The repetition of the 'cp' command is just for testing purposes, so that the exact same script can be tested in both the following cases. So, the script works perfectly (except the first 'cp' command for permission reasons) when the following command line is ran:
sh /home/pi/test-script.sh /var/lib/motion/snapshot.jpg
The snapshot is transmitted OK to the distant server. But when 'motion' is configured with the following command:
on_picture_save sh /home/pi/test-script.sh /var/lib/motion/snapshot.jpg
the script executes but neither the 'sudo' block nor the 'put' command work. I couldn't find any way to get a log of the execution. Supposing it was a problem of access rights, I splitted my script in the two following ones:
#!/bin/bash cp $1 /tmp/snapshot-copy.jpg sudo -u motion /bin/bash << EOF cp $1 /tmp/snapshot-copy.jpg EOF sudo -u pi /bin/bash << EOF sh /home/pi/transfer.sh /tmp/snapshot-copy.jpg EOF
#!/bin/bash export SSHPASS=MyServerPassword sshpass -e sftp -oBatchMode=no -oPort=9999 -b - pi@MyServerIPAddress << ! put $1 bye !
Then, same scheme as above: the file is transmitted in the case of the command line, but none of the 'sudo' blocks are executed when 'splitted-test-script.sh' is launched by 'on_picture_save'.
How to make it work?