Search code examples
node.jsbashshellgruntjsgrunt-shell

Is it possible to execute sh script passed as argument with grunt-shell plugin?


I have sh script on my local machine like the following:

// run_script.sh
#!/bin/sh -e
echo "Hello"

Is it possible to execute this script on remote server without copying it to? Something like the following:

grunt.initConfig({
  script_path: '/path/to/run_script.sh',

  shell: {
    makeDir: {
      command: '<%= script_path.sh %>'
    }
  }
});

Solution

  • If the script is deployed on the remote server, then you can call it through SSH. But you will need an SSHD started on the remote server. I advice you to configure key pair (as you want a non-interactive behavior and as openssh is designed to be interactive with password).

    ssh me@remote myScript.sh
    

    If the script is local, then you can copy it (scp) then execute it. Or you can execute a remote SHELL through SSH and send the local script through stdin

    cat myScript.sh | ssh me@remote bash
    

    In all cases, you will have to make strong assumptions about your environment (for example there is no sshd on windows machine...) If you explain the real use case behind the scene, maybe we will be able to suggest a better solution.

    EDIT: you want to run deployment script. As this installs a software, it's important to trust who is executing it (in particular in production). The general case is not simple...

    I advice you to look for juju/chef/puppet/ansible or another similar softwares. The last one is agentless. They can be a drop replacements for your grunt/shell homemade scripts, or you can mimic what you need.