AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-1121302

Matth's questions

Martin Hope
Matth
Asked: 2024-04-30 22:26:12 +0800 CST

环境变量LOGNAME或USER与有效用户id不对应

  • 7

我正在运行 Ubuntu 20.04.6 LTS 服务器。该服务器托管一些 Virtualbox 虚拟机,我每天使用 bash 脚本备份这些虚拟机。这工作得很好。当备份工作时,bash 脚本还会发送一些电子邮件,或者如果备份失败,我会通过电子邮件收到错误消息。

最近我开始收到一些电子邮件,Cron Daemon内容如下:

WARNING: Environment variable LOGNAME or USER does not correspond to effective user id.

主题向我展示了显然导致问题的脚本:Cron <user@server> /home/user/buprod.sh

该脚本是通过 crontab 作为普通用户而不是 root 用户执行的。更令人费解的是,我正在备份多个虚拟机,但我只从一个脚本而不是其他脚本中收到该警告消息。

我需要担心警告消息吗?我怎样才能首先避免它?

编辑(根据评论提供更多信息):

crontab 位于以下位置var/spool/cron/crontabs/matth并具有以下内容:

# For more information see the manual pages of crontab(5) and cron(8)
# m h  dom mon dow   command
SHELL=/bin/bash

04 2 * * 1 /home/matth/buproxy.sh
24 2 * * * /home/matth/buprod.sh

导致警告消息的脚本是buprod.sh并且具有以下内容:

#!/bin/bash

# =============== Set your variables here ===============
MYMAIL="[email protected]"
VMDIR="/home/matth/VirtualBox\ VMs/ERPNext\ Production/"
EXPORTDIR="/mnt/video/Backup/vmbackup/"
TEMPDIR="/home/matth/temp/"
VMTMPDIR="/home/matth/temp/ERPNext\ Production/"
LOGDIR="/home/matth/logs/"
LOGNAME="erpnextprod.log"
VM="ERPNext Production"
ERR="nothing"
SECONDS=0
STARTUP=1
COPY=1
# =======================================================
# No manual changes needed below this point
# =======================================================

LOGFILE="$LOGDIR$(date +"%Y-%m-%d-%T")-$LOGNAME"

cdt=$(date)
echo "${cdt}: Backing up VM ${VM}" >> $LOGFILE

# Get the vm state
VMSTATE=$(vboxmanage showvminfo "$VM" --machinereadable | grep "VMState=" | cut -f 2 -d "=")
cdt=$(date)
echo "${cdt}: ${VM}'s state is: ${VMSTATE}." &>> $LOGFILE

# If the VM's state is running or paused, save its state
if [[ $VMSTATE == \"poweroff\" ]]; then
  # skip the wait
  cdt=$(date)
  echo "${cdt}: VM ${VM} is already powered off" &>> $LOGFILE
  STARTUP=0
else
  # Shut down the VM
  cdt=$(date)
  echo "${cdt}: Shutting down the VM ${VM}" &>> $LOGFILE
  vboxmanage controlvm "$VM" acpipowerbutton
  if [ $? -ne 0 ]; then ERR="shutting down"; fi
  # cdt=$(date)
  # echo "${cdt}: Waiting 120 seconds to let the VM ${VM} shut down" &>> $LOGFILE
  # sleep 60s
fi

max_wait=$((SECONDS+120))
cnt=1
# Check state of VM
VMSTATE=$(vboxmanage showvminfo "$VM" --machinereadable | grep "VMState=" | cut -f 2 -d "=")
# Wait a maximum of 120 seconds to avoid an infinite loop in case the VM can't be stopped
while [[ $VMSTATE != \"poweroff\" ]]
do
   cdt=$(date)
   echo "${cdt}: ${cnt} loop(s) waiting for the VM ${VM} to shut down. Current state is ${VMSTATE}" &>> $LOGFILE
   # Wait a few seconds before checking again
   sleep 10s
   # Checking state of VM again
   VMSTATE=$(vboxmanage showvminfo "$VM" --machinereadable | grep "VMState=" | cut -f 2 -d "=")
   cnt=$(( $cnt + 1 ))
   # If loop has taken more than 60 seconds, break it to avoid an infinite loop
   if [ $SECONDS -gt $max_wait ]; then break; fi
done

# Check if VM is now powered off
VMSTATE=$(vboxmanage showvminfo "$VM" --machinereadable | grep "VMState=" | cut -f 2 -d "=")
cdt=$(date)
echo "${cdt}: ${VM}'s state is: ${VMSTATE}." &>> $LOGFILE

if [[ $VMSTATE == \"poweroff\" ]]; then
   # First copy it to a local directory to have a faster copy
   eval cp -R "${VMDIR}" $TEMPDIR
#   eval cp -R "${VMDIR}" $EXPORTDIR
   cdt=$(date)
   echo "${cdt}: VM ${VM} has been copied from ${VMDIR} to ${TEMPDIR}" &>> $LOGFILE
else
   ERR="not powered off"
   cdt=$(date)
   echo "${cdt}: Not exporting because the VM ${VM} is not powered off." &>> $LOGFILE
   COPY=0
fi

if [[ $STARTUP == 1 ]]; then
   vboxmanage startvm "$VM" --type headless &>> $LOGFILE
   if [ $? -ne 0 ]; then
      ERR="starting up"
   else
      cdt=$(date)
      echo "${cdt}: VM is started up again" &>> $LOGFILE
   fi
fi

# Calculate duration
duration=$SECONDS
duration="Operation took $(($duration / 60)) minutes, $(($duration % 60)) seconds." &>> $LOGFILE

if [[ $COPY==1 ]]; then
   # Now copy off-machine
   eval cp -R "${VMTMPDIR}" $EXPORTDIR
   cdt=$(date)
   echo "${cdt}: VM ${VM} has been copied from ${VMTMPDIR} to ${EXPORTDIR}" &>> $LOGFILE
   eval rm -R "${VMTMPDIR}"
   cdt=$(date)
   echo "${cdt}: Temporary directory ${VMTMPDIR} has been deleted" &>> $LOGFILE
   # Calculate duration
   duration=$SECONDS
   duration="Operation including copy to NAS took $(($duration / 60)) minutes, $(($duration % 60)) seconds." &>> $LOGFI>
fi

# Notify the admin
if [ "$ERR" == "nothing" ]; then
  MAILSUBJECT="VM ${VM} succesfully backed up"
  MAILBODY="Virtual Machine ${VM} was succesfully backed up!"
  MAILBODY="$MAILBODY"$'\n'"$duration"
#  MAILBODY=$(echo $MAILBODY && cat $LOGFILE)
else
  MAILSUBJECT="Error $ERR VM ${VM}"
  MAILBODY="There was an error ${ERR} VM ${VM}."
  MAILBODY=$(echo $MAILBODY && cat $LOGFILE)
fi

# Send the mail
echo "$MAILBODY" | mail -s "$MAILSUBJECT" $MYMAIL

编辑 2:提供下面的附加详细信息

我已经检查了脚本生成的日志文件。据了解,该错误似乎来自虚拟机的重新启动。

At 27: USER=, LOGNAME=matth
Fri May  3 11:38:01 AM HKT 2024: Backing up VM XGTERPNext
Fri May  3 11:38:02 AM HKT 2024: XGTERPNext's state is: "running".
Fri May  3 11:38:02 AM HKT 2024: Shutting down the VM XGTERPNext
Fri May  3 11:38:02 AM HKT 2024: 1 loop(s) waiting for the VM XGTERPNext to shut down. Current state is "running"
Fri May  3 11:38:07 AM HKT 2024: XGTERPNext's state is: "poweroff".
Fri May  3 11:38:23 AM HKT 2024: VM XGTERPNext has been copied from /home/matth/VMs/XGTERPNext/ to /home/matth/temp/
At 88: USER=, LOGNAME=matth
WARNING: Environment variable LOGNAME or USER does not correspond to effective user id.
Waiting for VM "XGTERPNext" to power on...
VM "XGTERPNext" has been successfully started.
Fri May  3 11:38:23 AM HKT 2024: VM is started up again
At 98: USER=, LOGNAME=matth

因此,我在该部分之前和之后添加了建议的打印输出,如下所示:

   echo "${cdt}: VM ${vm} has been copied from ${vmdir} to ${tempdir}" &>> $logfile
else
   err="not powered off"
   cdt=$(date)
   echo "${cdt}: Not exporting because the VM ${vm} is not powered off." &>> $logfile
fi
echo "At $LINENO: USER=$USER, LOGNAME=$LOGNAME" >> $logfile
if [[ $startup == 1 ]]; then
   vboxmanage startvm "$vm" --type headless &>> $logfile
   if [ $? -ne 0 ]; then
      err="starting up"
   else
      cdt=$(date)
      echo "${cdt}: VM is started up again" >> $logfile
   fi
fi
echo "At $LINENO: USER=$USER, LOGNAME=$LOGNAME" >> $logfile

当我在终端中手动运行脚本时,没有警告消息。当脚本通过 crontab 计划运行时,USER 为空,因此与 LOGNAME 不同。

而且,这条警告信息是从4月27日之后才出现的,此前从未出现过。我不确定,但可能我在 4 月 26 日运行了更新,并且发生了一些变化?

cron
  • 1 个回答
  • 262 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve