Linux 根运行cron任务可以';t读取www数据用户生成的.txt文件

Linux 根运行cron任务可以';t读取www数据用户生成的.txt文件,linux,bash,cron,root,Linux,Bash,Cron,Root,我有一个简单的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 e

我有一个简单的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
我的印象是,这仍然意味着该文件可以被其他用户读取? 如果我打开文件并读取内容,是否需要能够执行该文件

如有任何建议,将不胜感激。如果您愿意,我可以扩展并显示我的代码,但以前我调用bash脚本来编写文件时,它是有效的。。。这一次,它将用rwx将文件保存在root用户下,以便大多数用户读取。但这会导致php页面出现其他问题,因此不可选择。

您有:

 while read -r coinfile; do
 ...
我看不到任何迹象表明您正在读取
$file
。命令

read -r coinfile
将仅从标准输入读取(
-r
仅影响反斜杠的处理)。在cron作业中,如果我没记错的话,标准输入是空的或不可用的,这可以解释为什么
$coinfile
是空的

如果您确实读取了
$file
——例如,如果您的真实代码看起来像:

while read -r coinfile; do
    ...
done <$file
读取-r文件时
;做
...

做得很好,谢谢你的否决票,没有评论!我没有投反对票,但你需要发布更多信息,以便人们能够帮助你。事实上,这个问题是无法回答的。请发布您的bash脚本的内容、crontab、文件的完整路径以及您得到的确切错误。嘿,谢谢您的建设性评论:)我已经添加了代码,以显示正在发生的基本情况。没有错误消息,只是文件被读取为空。完整路径/home/test/customcoincode/queue/coinfile.txt是
#/bin/bash
在您的脚本中确实前面有空格,或者这是一个复制粘贴错误?如果在
while
循环的顶部之前添加
wc$file
,您会得到什么输出?好的,我已经包含了整个测试bash脚本,我将读取问题隔离到该脚本中。非常感谢您的回复。。。我觉得你可能需要我补充更多
while read -r coinfile; do
    ...
done <$file