我不时重复以下命令:
ssh username@servername
cd /projects/rails_project
bundle exec rails c production
我想创建一个 shell 脚本,并在一行中为运行生产控制台的此文件创建别名。如果我写了简单的脚本,而这个 3 命令是行不通的。
我该怎么做?
如果您不使用 ssh -t,Rails 控制台将不会提示您输入。你真正想要的是:
ssh -t username@servername "cd/projects/rails_project && bundle exec rails c production"
使用 -t " [...]可用于在远程机器上执行任意基于屏幕的程序 [...]"(根据 SSH 手册页。
只需将其作为参数发送:
ssh username@servername 'cd /projects/rails_project && bundle exec rails c production'
从man ssh
:
SYNOPSIS
ssh [options] [user@]hostname [command]
...
If command is specified, it is executed on the remote host instead of a
login shell.
...
When the user's identity has been accepted by the server, the server
either executes the given command in a non-interactive session or, if no
command has been specified, logs into the machine and gives the user a
normal shell as an interactive session. All communication with the remote
command or shell will be automatically encrypted.
更准确地说,如果您使用 bash 并且有 bundle command not found
错误,那么您已经在运行 bundle exec 之前设置了它,例如:
ssh -t <user>@<server> "cd /home/appfolder && /bin/bash --login bundle exec rails c -e production"