我知道这CRON_TZ=
将为所有命令设置 cron 运行的时区。
不过,我希望一些 cron 条目(而不是全部)在特定时区运行。
这可能吗?如果不可能,您有什么(黑客式的)解决方法吗?
我知道这CRON_TZ=
将为所有命令设置 cron 运行的时区。
不过,我希望一些 cron 条目(而不是全部)在特定时区运行。
这可能吗?如果不可能,您有什么(黑客式的)解决方法吗?
我如何暂停所有 crontab 的使用,直到系统重新启动?系统运行 ubuntu 24.04。我说所有使用是因为系统上的几个 ID 都有 crontab。
我正在运行 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 日运行了更新,并且发生了一些变化?
该命令crontab -e
已停止在非 root 用户帐户下工作并给出错误:
/bin/sh: 1: mvim: not found
crontab: "mvim" exited with status 127
然而,它仍然适用于root sudo
。不知道有什么变化,只是常规aptitude
升级。
更新:我在我的.zshrc
. 注释掉就解决了这个问题。
# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR='vim'
else
export EDITOR='mvim'
fi
我已经设置了一个新的 EC2 实例。一切都运转良好。今天早上我已经设置了 cron,下面是 cron 的示例
*/5 * * * * /usr/bin/php https://www.xyz.in/api/cron/index.php
我已经测试了很多次,但我的 cron 不工作。如果我手动点击 cron url 那么它就可以工作。我需要知道是否必须在 EC2 实例中设置任何设置才能运行 cron。
Ubuntu 版本22.04.3
LTS
我正在查看 Ubuntu 如何选择默认编辑器,我注意到要设置用于编辑的默认编辑器crontabs
,我使用while 来设置我使用的select-editor
默认编辑器。有谁知道这两者的具体区别?有很多文档分别解释了每一个,但我一直无法弄清楚两者之间的区别或者为什么我必须同时设置两者。visudo
update-alternatives --config editor
为了完整起见,我想指出我知道如何通过设置 EDITOR 和 VISUAL 环境变量来设置默认编辑器。
如果我运行crontab -e
,Ubuntu 会显示当前用户的默认 cron 作业:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
请注意这样说的部分:
crontab 作业的输出(包括错误)通过电子邮件发送给 crontab 文件所属的用户(除非重定向)。
如何查看此输出?什么电子邮件与我的 Linux 用户相关联?
如果我查看grep CRON /var/log/syslog
,我会看到这样的行:
Feb 10 17:27:01 desktop-ubuntu CRON[36079]: (CRON) info (No MTA installed, discarding output)
我假设 MTA 是指邮件传输代理。输出似乎被丢弃了。
就个人而言,我希望将输出保存在某个地方,而无需转到 Internet 上的真实电子邮件地址。但我的问题更多是关于理解默认行为。