4

I'm making script for backing up data from server, it's gonna use ssh and scp many times. My private key is password-protected, but I'm using environment without any ssh agent.

I'd like to put condition to script, that check whether script is launched using this answer. If there is no ssh agent running, I would like to use exec ssh-agent <original script relaunch>.

How to do that?

  • it looks like you answered your own question :) – Jeff Schaller Sep 5 '15 at 13:20
  • Well... no, I don't know how to execute script again with exact cmdline arguments, that was used for first time :) – kravemir Sep 5 '15 at 13:32
  • What happens when ssh-agent always fails in 1 second? Endless loop? Why not put thecode of your current script inside a function and call that function again-and again-and-again and stop after some failures? – Walter A Sep 6 '15 at 19:44
4

To wrap the script itself use $0 (program/script name) and $@ (the expanded list of arguments: same as "$1" "$2"...).

if something; then
    exec ssh-agent "$0" "$@"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.