1

I try to make a program to download from a specific URL a file. Check if exist and rename the original

when I run it with sudo command everything works fine. I said.. wow!! DONE! and when I add it to cron tab... boom! nothing works.

I read a post that says make a script and run it from there so I did

sudo crontab -e -> */5 * * * * /home/pi/guardian/run_update.sh
run_update.sh -> #!/bin/sh sudo /usr/bin/python /home/pi/guardian/updater.py

issues that i get:

  1. i couldn't rename a file with os.system("sudo mv " + originalfile + ' ' + renamedfile) not error raised
  2. I couldn't rename a file using the os.rename command in both cases crash with no error the same, no errors

    *the above error i fix it by using smaller names on variables

    os.system("sudo mv " + s1 +' '+s2)

  3. I couldn't store the file downloaded from the web using

:

testfile = urllib.URLopener()
urllib.urlretrieve(s1, s2)

or by using

getfile = urllib2.URLopener(fromwhere+downloadfiles[i])
with open(downloadfiles[i],'wb') as output:
    data=getfile.read(4096)
    if data:
        output.write(data)
    else:
        break:

Please advice

After many tries i find out that the program run perfect on command line and almost not at all when runing from crontab, so with the technic "try and seek" at the point that i am now, the problem is that i cant store downloaded file

ANSWER from Vikash Singh thanks a lot sir!!

The program was trying to save the downloaded file to "/root" and after that was only a matter of stored location path.

5
  • cron commands are run as root. Keep this in mind. Try to run your script as root and you will probably find the problem Commented Aug 10, 2017 at 7:57
  • I did that change too but it was the same. But even if i run them as simple user e.g sudo python updater.py they work as expected... Commented Aug 10, 2017 at 8:40
  • pls post your script Commented Aug 10, 2017 at 9:17
  • sure! the script that is working right now is the following: #!/bin/sh sudo /usr/bin/python /home/pi/guardian/updater.py Commented Aug 10, 2017 at 9:48
  • @mbieren do you want to post the program too? its a bit big Commented Aug 10, 2017 at 10:36

1 Answer 1

2

Why don't you update the crontab to directly run the python code. instead of invoking shell script which invokes python script.

sudo crontab -e -> */5 * * * * /usr/bin/python /home/pi/guardian/updater.py

UPDATE: after a good long discussion in comments we found that the code when run from crontab was writing file in root directory. As crontab default directory is '/' for root user.

Fix was to add full path for writing file. this full path was the expected path where we were looking for file.

Another option would have been to change crontab to first change directory to expected directory and then run the program :

sudo crontab -e -> */5 * * * * cd <to program path> && /usr/bin/python /home/pi/guardian/updater.py

Explanation for why it was working when the program was run from command line with sudo.

When the program was executed in the command line the user would be running the program like:

$ cd /path/to/program/
/path/to/program @ user$ sudo python program.py
# this program will create file in current directory 
# which is where we were expecting the output file to be.

When we were running it with crontab root user. It would run in the root directory as that's the running directory for program. In terminal it would be like:

$ sudo /usr/bin/python /path/to/program/program.py

This would create output file in / directory

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion or debugging sessions; this conversation has been moved to chat. Please edit the important information into your question and/or answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.