linux - Cron running Python script : Permission Errors -


i have python program opens file testlog.txt, should in same directory program file, , appends current date , time stamp it.

edit : file open function f = open(file,'a') .

let's program resides in /home/user1/pyscript/ program print date/time it's output. when run script within directory runs fine.

running / directory gives me permission error on writing file. issue may coming want understand logic behind it.

i have crontab entry script run every minute. entry looks this:

*/1 * * * * sudo python /home/user1/pyscript/test.py >> /home/user1/pyscript/test.txt 

when job runs writes print output file test.txt not write file testlog.txt

my question if cron job can write 1 file how can not have permission write other file.

ls -l gives me following directory program file resides.:

drwxr-xr-x  6 ec2-user users     4096 jul 27 20:29 pyscript 

first things first: instead of putting sudo in front of cronjob, make sure script really, really needs run root. safer way @ script needs touch, create new user on system , give user exact permissions needs execute script, replace sudo users name.

now actual problem. wrote

have python program opens file (testlog.txt (in same directory program file) , appends current date , time stamp it.

i'm going assume, based on wording, opening testlog.txt prefixed ./? in case ask take @ / folder. should find testlog.txt there, since script runs under root script should have been able create file.

the reason permission error when trying run script manually / not using sudo , not logged in root @ time.

instead try using path directory script resides in and testlog.txt file should created in:

os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

this based on 2 answers:


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -