0

I'm trying to build a script that automatically takes a file on the remote server and replace with crontab file, but I get permission denied.

My idea is to create a shell function for it:

update_crontab() {
  SSH_HOST=$1
  FOLDER=$2

  {
    if ssh -o "BatchMode yes" $SSH_SUDO_WHITOUT_PASS@$SSH_HOST "[ -f $FOLDER/crontab ]"
    then
      # Folder exists replace crontab with new file
      ssh -o "BatchMode yes" $SSH_SUDO_WHITOUT_PASS@$SSH_HOST "sudo cat $FOLDER/crontab > /etc/crontab"
      echo "crontab overwirte from $FOLDER/crontab"
    fi
  } || {
    echo "Error - Folder not exists"
    exit 1
  }
}
  • consider using scp instead? – Jeff Schaller Feb 11 at 14:19
  • how can i use scp local between 2 files? my new file is local on the remote server, and i need to replace the remote servers crontab file, not from my machine to remote server. – ParisNakitaKejser Feb 11 at 14:33
  • maybe I misunderstood; you're trying to copy the remote system's $FOLDER/crontab over the remote system's /etc/crontab? no "local" files involved at all? – Jeff Schaller Feb 11 at 14:34
  • yes currect, have the file on my remote server. – ParisNakitaKejser Feb 11 at 14:35
  • @Rui, you took the only "question" out of their question. Perhaps leave them with their actual question, if you're going to go to the effort of removing it? – Jeff Schaller Feb 11 at 14:35
1

Since both files are remote, you can simply:

  ssh ... "sudo cp $FOLDER/crontab /etc/crontab"

... which avoids the "sudo redirection" problem where only the cat has elevated privileges, and your normal user shell does the > /etc/crontab redirection.

  • Thanks so much, so simple and i have not thinking about it. – ParisNakitaKejser Feb 11 at 14:38

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.