Script using scp

Hello,
I am an student trying to make his final project and I wanna make a script to transfer a file using scp. The problem is that I need that executes when the computer starts (before session login), Ive tried this, if I execute it when im logged in it works, but not when the computer starts:

#!/bin/sh

scp /path/to/local/targetfile.txt remoteuser@remotehost:/path

Thanks for replys and sorry for my bad english.

What is your system? Do you have cron?

1 Like

Hello Corona688, I am gonna tell you what my project consists of:

We have a script that checks from a remote DB a value (0 or 1). If a laptop is stole (1) if not (0).

If 1: Ubuntu client makes a photo (when computer starts) and calls another script to send the photo to the server.
If 0: Ubuntu doesn't do the photo.

The problem came when we trying to send the photo, it doesn't work. If we execute it when we are logged in it works perfectly. We have been trying to solve this for like 1 week and no results. We dont wanna use cron. Thanks.

I presume you have key-based authentication set up so it doesn't ask for a password...

If you're logged in, it finds the key in ~/.ssh/ by itself.

If you're not logged in, it will need to be told where the keys are with -i identityfile. It will need to have the right permissions for whatever user it's running as.

If you're not running it with cron, what are you running it with? Have you put it in as a system service or some such?

1 Like

Nice, it could be the problem, can you make a simple example of -i identityfile?
Thanks.

scp -i /path/to/identityfile /path/to/local/targetfile.txt remoteuser@remotehost:/path

What /path/to/identityfile is depends on what your identity file is. It will need to be owned by whatever user you're running it as, and chmod 0600, and in a folder also owned by it set 0700.

we tried:

scp -i /home/alumne/.ssh/id_rsa /00000005.jpg furts@192.168.12.235:/

It returns permission denied. We gave chmod 0600 to file and 0700 to directory.

We are executing the script as system service.

THanks.

Whatever user its running as, it's almost certainly not running as alumne. As warned, the folder and the file probably have the wrong owners. ssh/scp are picky about that.

Assuming its running as root, make a copy /root/.ssh/id_rsa, owned by root, with the correct permissions, for it to use.

We solved that using crontab. We didnt found another solution to the problem.

We did:

crontab -e
@reboot /etc/init.d/script.sh

Thanks Corona688

Crontab is probably a better place for it. Glad you got it working.