我有一个简单的php页面,可将文件写入服务器。
// open new file
$filename = "$name.txt";
$fh = fopen($filename, "w");
fwrite($fh, "$name".";"."$abbreviation".";"."$uid".";");
fclose($fh);
然后,我有一个cron作业,我知道该作业以root身份作为测试运行,并且需要它。
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
cronjob是一个bash脚本,可以检测文件是否存在,但似乎无法读取文件的内容。
#!/bin/bash
######################################################
#### Loop through the files and generate coincode ####
######################################################
for file in /home/test/customcoincode/queue/*
do
echo $file
chmod 777 $file
echo "read file"
while read -r coinfile; do
echo $coinfile
echo "Assign variables from file"
#############################################
#### Set the variables to from the file #####
#############################################
coinName=$(echo $coinfile | cut -f1 -d\;)
coinNameAbreviation=$(echo $coinfile | cut -f2 -d\;)
UId=$(echo $coinfile | cut -f3 -d\;)
done < $file
echo "`date +%H:%M:%S` - $coinName : Your Kryptocoin is being compiled!"
echo $file
echo "copy $coinName file to generated directory"
cp -b $file /home/test/customcoincode/generatedCoins/$coinName.txt
echo "`date +%H:%M:%S` : Delete queue file"
# rm -f $file
done
echo $file
识别文件存在echo $coinfile
为空白但是当我在终端中
nano ./coinfile.txt
时,我可以清楚地看到那里有文字我运行
ls -l
,我看到该文件具有权限-rw-r--r-- 1 www-data www-data
我的印象是,这仍然意味着文件可以被其他用户读取吗?
如果我打开文件并阅读内容,我是否需要能够执行文件?
任何建议将不胜感激。我可以根据需要扩展并显示我的代码,但是在我调用bash脚本写入文件之前它已经起作用了……那时候它将以rwx将文件保存在root用户下,然后可以读取。但这随后在php页面中引起了其他问题,因此不是一种选择。
最佳答案
你有:
while read -r coinfile; do
...
我没有迹象表明您正在阅读
$file
。命令read -r coinfile
只会从标准输入中读取(
-r
仅影响反斜杠的处理)。在cron作业中,如果我没记错的话,标准输入为空或不可用,这将解释为什么$coinfile
为空。如果您确实确实是从
$file
读取的-例如,如果您的真实代码如下所示:while read -r coinfile; do
...
done <$file
那么您需要向我们展示您的整个脚本,或者至少显示出问题的自包含脚本。实际上,无论是否存在问题,您都需要向我们展示您的整个脚本。
http://sscce.org/
关于linux - 根目录运行的cron任务无法读取www-data用户生成的.txt文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18835497/