我正在尝试获取关闭脚本以使用Google Cloud Computing VM执行.
I am trying to get a shutdown script to execute using a Google Cloud compute VM.
我在运行 gcloudcompute connect-to-serial-port startup-test-v
Apr 8 22:01:25 startup-test-v shutdown-script: INFO Starting shutdown scripts. Apr 8 22:01:25 startup-test-v shutdown-script: INFO Found shutdown-script in metadata. Apr 8 22:01:26 startup-test-v shutdown-script: INFO shutdown-script: No change requested; skipping update for [startup-test-v]. Apr 8 22:01:27 startup-test-v shutdown-script: INFO shutdown-script: Return code 0. Apr 8 22:01:27 startup-test-v shutdown-script: INFO Finished running shutdown scripts.我从命令行创建可抢占实例,然后在GUI中将其关闭.
I create the preemptible instance from the command line and shut it down in the GUI.
gcloud compute instances create $INSTANCE_NAME \ --zone=$ZONE \ --image-family=$IMAGE_FAMILY \ --image-project=deeplearning-platform-release \ --maintenance-policy=TERMINATE \ --machine-type=$INSTANCE_TYPE \ --boot-disk-size=50GB \ --metadata="install-nvidia-driver=True" \ --preemptible \ --scopes="storage-rw,cloud-platform" \ --metadata-from-file="shutdown-script=gce/shutdown_test.sh"shutdown_test.sh 很简单:
#!/bin/bash echo "+++ Shutdown test +++" exit 0启动脚本按预期运行.我尝试过将-元数据来自文件标志也替换为-元数据来自文件shutdown-script = gce/shutdown_test.sh 改变.
Startup scripts are working as expected. I've tried swapping the --metadata-from-file flag to --metadata-from-file shutdown-script=gce/shutdown_test.sh as well, with no change.
想法?看来GCE正在找到关闭脚本,但没有执行它.
Ideas? It seems GCE is finding the shutdown script, but not executing it.
推荐答案关闭脚本不提供任何输出到串行控制台或当前SSH终端控制台,因为它以root用户身份运行,并且为 stdout 既不路由到/dev/tty * 也不路由到/dev/ttyS * .
Shutdown script does not provide any output neither to serial console or current SSH terminal console because it is running as root user and it's stdout is not routed neither to /dev/tty* or /dev/ttyS*.
为确保关闭脚本确实有效,您可以将VM元数据键 shutdown-script 放入如下内容:
To ensure that shutdown script does actually work, you might put into the VM metadata key shutdown-script something like below:
#!/bin/bash ofile=/var/tmp/shutdown.txt echo "+++ Running shutdown script +++" echo "id = $(id)" > $ofile echo "script_file path = $(realpath $0)" >> $ofile echo "script_file rights, user, group = $(stat -c "%A %U %G" $0)" >> $ofile下一步,同时连接到串行控制台和SSH,以准备观看关闭脚本提供的任何输出的缺失.
Next connect to both the Serial Console and SSH to get ready to watch the absence of any output provided by the shutdown script.
然后停止并启动VM.
最后,您将看到关闭脚本实际上已经以root用户身份工作并留下了日志文件:
In the end you will see that shutdown script has actually worked as root user and left a log file:
$ ls -l /var/tmp/shutdown.txt rw-r--r-- 1 root root 165 Apr 9 18:40 shutdown.txt $ cat /var/tmp/shutdown.txt id = uid=0(root) gid=0(root) groups=0(root) script_file path = /tmp/metadata-scripts196132089/shutdown-script script_file rights, user, group = -rwxr-xr-x root root关机脚本适用于可抢占实例,与普通实例相同.区别在于,前者的关闭期较短(30秒).
Shutdown script works for preemptible instances the same as for normal instances. The difference is that the former have shorter shutdown period (30 seconds).
更多推荐
关闭脚本未在Google Cloud VM上执行
发布评论