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.
What am I missing here?
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.
What am I missing here?
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.
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.
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
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
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
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.