我对 ubuntu 服务器真的很陌生,这周我在 crontab 方面遇到了很多问题,我真的无法弄清楚它出了什么问题,我尝试了我在这里看到的一切,但都无济于事。
所以,我有一个脚本,理论上应该在网络不活动几分钟后关闭服务器。该脚本执行没有任何问题,并且如果手动执行,则可以完美运行。当我尝试设置 cron 来执行此脚本时,我遇到的问题发生了。
这是代码
#!/bin/bash
set -o nounset -o errexit -o pipefail
MARKER_FILE="/tmp/ssh-inactivity-flag"
STATUS=$(netstat | grep ssh | grep ESTABLISHED &>/dev/null && echo active || echo inactive)
if [ "$STATUS" == "inactive" ]; then
if [ -f "$MARKER_FILE" ]; then
echo "Powering off due to ssh inactivity."
rm --force "$MARKER_FILE"
/sbin/poweroff # See https://unix.stackexchange.com/a/196014/56711
else
# Create a marker file so that it will shut down if still inactive on the next time this script …Run Code Online (Sandbox Code Playgroud)