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}


${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 3 '17 at 19:51
  • 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 3 '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. – roaima Apr 3 '17 at 20:22
  • 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 3 '17 at 20:24
  • 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 3 '17 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.