英文:
Cron displaying error despite bash script running fine manually (Linux)
问题
在Cron中运行Bash脚本时遇到的错误是因为脚本无法找到指定的输入文件。要解决这个问题,你可以采取以下措施:
- 使用绝对文件路径:在Cron作业中,最好使用绝对文件路径,而不是相对路径,以确保脚本可以找到文件。你可以使用
$HOME
来引用当前用户的主目录。修改脚本中的文件路径为:
hw1_input="$HOME/Documents/GitHub/BFOR206/hw1_input.txt"
-
检查文件权限:确保输入文件
hw1_input.txt
对于Cron作业中运行的用户可读取。你可以使用chmod
命令更改文件权限,以确保脚本可以访问它。 -
使用绝对路径执行Cron作业:在Cron作业中,使用完整的绝对路径来执行Bash脚本。你已经在Cron中使用了绝对路径,所以这部分是正确的。
在采取这些措施之后,重新运行Cron作业,应该可以解决文件路径问题。如果还有其他问题,请查看日志以获取更多信息,以便进一步调试。
英文:
So, I'm working on a bash script on a Linux VM for a class that pings multiple ip addresses from an input file and emails myself if there is any errors, while sending the results (successful or failed) to an output log. To get full credit we need to have it run every 5 minutes using cron. If I run it manually with the bash command, it works fine, doing all of the things it is supposed to do, but when it runs with cron I receive this error:
/home/kali/Documents/GitHub/BFOR206/hw1.sh: line 33: hw1_input.txt: No such file or directory
Here is my code:
#!/bin/bash
# File path for input (ip addresses)
hw1_input="~/Documents/GitHub/BFOR206/hw1_input.txt"
# Loop through and read each address in the file
while read -r address; do
# Ping the address and save the output
ping -c 1 $address > /dev/null
# Check if ping was normal or failed
if [ $? -eq 0 ]; then
# If the ping is normal, log to a file
echo "Ping successful for $address at $(date)" >> hw1_output.log
else
# If the ping failed. log the error to a file
echo "Ping failed for $address at $(date)" >> hw1_output.log
# Send an email notification to self about error
echo "Ping failed for $address at $(date)" | mail -s "Ping Error!" kali@kali
fi
done < "hw1_input.txt"
The error is from the last line.
I'm very new to scripting so if anyone has any ideas of ways I can try and fix this, please try and explain it in simple terms. I've spent countless hours already trying to troubleshoot it myself so posting here is kind of my last resort.
If it helps, this is the script I am using to run the cron:
*/5 * * * * bash ~/Documents/GitHub/BFOR206/hw1.sh
答案1
得分: 0
问题一:
- 引用文件路径会阻止bash将
~
扩展为您的主目录。在对hw1_input
进行赋值时不要引用。
问题二:
- 文件名中不包括目录路径,你应该使用
hw1_input
变量。
#!/bin/bash
# 输入文件的文件路径(IP地址)
hw1_input=~/Documents/GitHub/BFOR206/hw1_input.txt
# 循环读取文件中的每个地址
while read -r address; do
# 对地址进行ping操作并保存输出
ping -c 1 $address > /dev/null
# 检查ping是正常的还是失败的
if [ $? -eq 0 ]; then
# 如果ping正常,记录到文件中
echo "Ping successful for $address at $(date)" >> hw1_output.log
else
# 如果ping失败,记录错误到文件中
echo "Ping failed for $address at $(date)" >> hw1_output.log
# 发送电子邮件通知自己有错误发生
echo "Ping failed for $address at $(date)" | mail -s "Ping Error!" kali@kali
fi
done < "$hw1_input"
英文:
Two problems:
- Quoting the pathname prevents bash from expanding
~
into your home directory. Don't quote in the assignment tohw1_input
. - You don't have the directory path in the filename you're redirecting from. You should use the
hw1_input
variable.
#!/bin/bash
# File path for input (ip addresses)
hw1_input=~/Documents/GitHub/BFOR206/hw1_input.txt
# Loop through and read each address in the file
while read -r address; do
# Ping the address and save the output
ping -c 1 $address > /dev/null
# Check if ping was normal or failed
if [ $? -eq 0 ]; then
# If the ping is normal, log to a file
echo "Ping successful for $address at $(date)" >> hw1_output.log
else
# If the ping failed. log the error to a file
echo "Ping failed for $address at $(date)" >> hw1_output.log
# Send an email notification to self about error
echo "Ping failed for $address at $(date)" | mail -s "Ping Error!" kali@kali
fi
done < "$hw1_input"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论