How to scp with init script?

Hi,
I have created an init script to start another script at boot time. Works as expected.
(OpenWrt 21.0.2 on NanoPi R2S)

The script started by the init.d script is working as expected, until it reaches an scp command.

So I have stripped down the script just for testing to this one.

#!/bin/sh
echo "starting"
scp /tmp/text.txt r2s.far.xa:/tmp/rtext.txt
echo "finished"

When running the script from OpenWrt CLI everything works as expected. The file is copied without any further question. (keys are exchanged for user root)
But when started by the init.d script nothing happens.
When looking at process list I found this:

$ ps w

...
22988 root      1012 S    scp /tmp/text.txt r2s.far.xa /tmp/rtext.txt                                                                                                  
22989 root      1040 S    /usr/bin/dbclient r2s.far.xa scp -t /tmp/rtext.txt
...

/etc/init.d/myscript

#!/bin/sh /etc/rc.common                                                                                                                                               
USE_PROCD=1                                                                                                                                                            
START=95                                                                                                                                                               
STOP=01                                                                                                                                                                
start_service() {                                                                                                                                                      
    procd_open_instance                                                                                                                                                
    procd_set_param command /bin/sh "/usr/bin/myscript.sh"                                                                                                           
    procd_close_instance                                                                                                                                               
}

What to do to get scp (and ssh -t afterwards) running?

Henning

Try /usr/bin/scp ... in your script.

Why do use procd style of init script, if you are not launching any daemons?
Might be much simpler in the old style.

Examples of init scripts that run just a few commands at boot

1 Like

@bluewavenet
Tried /usr/bin/scp instead of scp. No difference.

@hnyman
My original script contains a endless loop. So I have to start it as daemon of course.
Finaly using procd is much easier. I.e. nothing to care about pid's and so on.
As I said I have stripped it down to only a few lines to reproduce the problem.

Invoke scp with -i fullpathtoprivatekeyfile rather than counting on it being found from the home directory. Also the dropbear client requires private key data in a different format than OpenSSH, the dropbearconvert utility must be run on them. I assume you've already tested the ability to scp from the command line.

This means your script will start first, the network will not be ready to use. Even 95 that the OP used is not necessarily late enough.

Sure, I just added links to examples of simple old-style init scripts :wink:

Ok, changed the script as follows. But did not work when startet via init script

#!/bin/sh
echo "starting"
/usr/bin/scp -i /root/.ssh/id_dropbear /tmp/text.txt r2s.far.xa:/tmp/rtext.txt
echo "finished"

Entering this command on CLI works ...

/usr/bin/scp -i /root/.ssh/id_dropbear /tmp/text.txt r2s.far.xa:/tmp/rtext.txt

Note that init scripts are run headless at boot, so echo is pretty meaningless. That can also cause trouble otherwise if the script contents expect any interaction.

You might also try there a detached subshell, so that the init script can finish.
Launch the scp script from kinitscript with
(script-launch)&

Is there difference between automatic start of the init script at boot and a manual trigger via /etc/init.d/yourinitscript start ??

Here is my way to solve the problem.

First I add a line to the init script:

procd_set_param stderr

With this addition line I got a message in the log.

host is not in the trusted host file

Where the hell is the file supposed to be? Then I found a hint.

Hm, seems that environment isn't set as assumed.
So finaly I added a "env" line to the init script.

#!/bin/sh /etc/rc.common                                                                                                                                               
USE_PROCD=1                                                                                                                                                            
START=95                                                                                                                                                               
STOP=01
PROG=/usr/bin/myscript.sh                                                                                                                                                             
start_service() {                                                                                                                                                      
    procd_open_instance
    procd_set_param env HOME=/root USER=root
    procd_set_param command "$PROG"
    procd_set_param respawn  # respawn the service if it exits
    procd_set_param stderr 1 # forward stderr to logd
    procd_close_instance                                                                                                                                               
}
1 Like

Yes, this is annoying. I have had to set these in my init scripts also. There is a bug tracker item about it but nobody has picked it up.

Yes, the reasoning for closing the issue is a bit strange.

You should not rely on user specific environment variables during system init.

Specially when using ssh / scp / dropbear as a system service there is no other user logging in ...

And a issue for adding a option to specify known_hosts location to dropbear was rejected too. https://github.com/openwrt/packages/issues/5583

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.