问题描述
我试图在启动期间使用元数据键startup-script"启动 tmux.我试图在启动期间执行 tmux new-session -d -s toto
,但是当我之后执行 tmux ls
时,我没有看到任何 tmux 会话.
I'm trying to start a tmux during startup with the metadata key "startup-script". I'm trying to execute tmux new-session -d -s toto
during the startup, but when I do a tmux ls
afterwards I don't see any tmux session.
我在这里遗漏了什么?
推荐答案
您可能在这里缺少一些先决条件:
There are few pre-requisites you might be missing here:
程序应该安装在映像上.标准映像附带了一组最少的程序.
The program should be installed on the image. The standard image comes installed with a bare minimal set of programs.
启动脚本在每次启动实例时运行,并以 root
身份运行.因此,如果您直接从启动脚本运行 tmux
,它将以 root 用户身份启动一个新的 tmux
会话.这不是您最想要的.
The startup script runs every time the instance is started and it runs as root
. So if you just run tmux
directly from the startup script, it will start a new tmux
session but as root user. This is not what you want mostly.
话虽如此,这将起作用(我已经给出了使用 gcloud
的示例,但您也可以将类似的逻辑应用于 REST API 或 Cloud Console):
Having said that, this will work (I've given examples using gcloud
but you can apply the similar logic to REST APIs or the Cloud Console as well):
所有这一切都可以使用这个启动脚本来处理:
All of this can be taken care using this startup script:
(hash tmux 2>/dev/null || (apt-get update && sudo apt-get -y install tmux)) && sudo -H -u USERNAME tmux new-session -d -s toto
以上命令将安装 tmux(如果尚未安装),然后启动一个名为 toto
的新分离的 tmux 会话.
The above command will install tmux if not already installed and then start a new detached tmux session named toto
.
你可以在创建实例的时候设置这个启动脚本:
You can set this startup script at the time of instance creation:
gcloud compute instances create VM_NAME --metadata 'startup-script=(hash tmux 2>/dev/null || (apt-get update && sudo apt-get -y install tmux)) && sudo -H -u USERNAME tmux new-session -d -s toto' --zone ZONE_NAME --project PROJECT_NAME
或稍后随时更新现有 VM 的元数据:
or update the metadata anytime later for an existing VM:
gcloud compute instances add-metadata vm-1 --metadata 'startup-script=(hash tmux 2>/dev/null || (apt-get update && sudo apt-get -y install tmux)) && sudo -H -u USERNAME tmux new-session -d -s toto' --zone ZONE_NAME --project PROJECT_NAME
您始终可以在 VM 上重新运行启动脚本(无需重启虚拟机):
You can always re-run the startup script on the VM (without restarting the VM):
$ sudo google_metadata_script_runner --script-type startup
有关启动脚本的完整文档可在此处获得.
The complete documentation about Startup scripts is available here.
这篇关于Google Compute Engine - 使用启动脚本启动 tmux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!