0

I am running a bash script as a cron job. The thing is that I use ls in the script and it says that absolute path file doesn't exist. But it exists:

function get_curr_video_size()
{

    curr_size=`ls -l ${video_name} | awk '{print $5}'`
    echo ${curr_size}

}
curr_size=`get_curr_video_size`

${video_name} is get by another function at earlier point, so it can't be non-existent during get_curr_video_size call.

Error is:

    ls: cannot access /home/pi/draft_videos/03_04_2017/test_03_04_2017_22:05:19.mp4: No such 
file or directory`

But the file exists. When I ls it in terminal it is there:

ls -l /home/pi/draft_videos/03_04_2017/test_03_04_2017_22:05:19.mp4
-rw-r--r-- 1 pi pi 0 Apr  3 22:05 /home/pi/draft_videos/03_04_2017/test_03_04_2017_22:05:19.mp4

If I run my script in a terminal instead as a cron job, it is ok. Seems like cron messes up things somehow, but I can't understand why.

I am using Raspbian Jessie on RPI.

  • Is that the real filename or are there actually spaces in the name? – thrig Apr 03 '17 at 19:51
  • @thrig this is the real file name, I don't think there are whitespaces in it, I copied it directly from the message in terminal and it was found – CuriousGuy Apr 03 '17 at 19:54
  • Please include your cron definition – Chris Davies Apr 03 '17 at 20:00
  • @roaima well, its kinda complicated, because my cron job actually runs a pyhon script, which using subprocess is starting the script I described. – CuriousGuy Apr 03 '17 at 20:03
  • Is there any possibility whatsoever that you could have two instances of the script running with the same filename at the same time? – Chris Davies Apr 03 '17 at 20:18
  • 1
    In shell, to get the size of a file, try stat -c '%s' FILENAME or du or wc -c. In Python, use os.stat. Not an answer to what's happening, just a pointer to a much better way to accomplish your goal – derobert Apr 03 '17 at 20:20
  • 1
    What if you simplify the whole thing with curr_size=$(stat -c %s "$video_name")? Notice that when a variable is used it's used in double quotes, and backticks have been replaced by the modern $(...) construct. – Chris Davies Apr 03 '17 at 20:22
  • I am gonna try the suggested ways of getting the file size – CuriousGuy Apr 03 '17 at 20:23
  • 1
    Also: What kind of system is this running on (personal workstation? Big institution set up?). Is the cron job out of your personal crontab, or a system one? And as a troubleshooting step, does installing a cron job that just does the ls -l work? – derobert Apr 03 '17 at 20:24
  • @derobert It is run on my personal Raspberry Pi system. Yes, I tried a cron job with only ls -l and it worked. Hmmm, maybe I am missing something. I'll try to investigate more and will let you know guys, Thanks for the suggestions! – CuriousGuy Apr 03 '17 at 20:30
  • 2
    @CuriousGuy The fact that it's your personal RPi seems to rule out anything complicated (e.g., different filesystem namespaces or some sort of access control system) going on. Most likely, it's simply that the path doesn't exist. E.g., because there is some weird character in the middle of it. – derobert Apr 03 '17 at 20:34
  • Wow, guys, it really doesn't exist. I did a ls of the directory and it is empty at the time of the ls -l ${video_name}. – CuriousGuy Apr 03 '17 at 20:46

0 Answers0