0

I have a python program which basically opens a file testlog.txt, which should be in the same directory as the program file, and appends the current date and time stamp to it.

EDIT : The file open function is simply f = open(file,'a') .

Let's say the program resides in /home/user1/pyscript/ the program will print the date/time as well as it's output. Now when I run the script from within that directory it runs fine.

Running it from / directory gives me a permission error on writing the file. This is where my issue may be coming from but I want to understand the logic behind it.

I have a crontab entry for the script to run every minute. The entry looks like this:

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

When the job runs it actually writes the print output to the file test.txt but it does not write to file testlog.txt

My question is if the cron job can write to one file how can it not have permission to write to the other file.

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

drwxr-xr-x  6 ec2-user users     4096 Jul 27 20:29 pyscript
NewGuyInJava
  • 190
  • 1
  • 13

1 Answers1

1

First things first: Instead of putting sudo in front of your cronjob, make sure the script really, really needs to run as root. A safer way would be to look at what the script needs to touch, create a new user on the system and give that user the exact permissions it needs to execute the script, then replace sudo with that users name.

Now to your actual problem. You wrote

have a python program which basically opens a file (testlog.txt (in the same directory as the program file) and appends the current date and time stamp to it.

I'm going to assume, based on the wording, that you are opening the testlog.txt with a prefixed ./? In which case I would ask you to take a look at your / folder. You should find a testlog.txt there, since your script runs under root the script should have been able to create that file.

The reason you get a permission error when trying to run the script manually from / is that you are probably not using sudo and you are not logged in as root at that time.

Instead try using this to get the path to the directory where your script resides in and where the testlog.txt file should be created in:

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

this is based on two answers:

Steffen Winkler
  • 2,666
  • 2
  • 33
  • 55
  • The file open was without any directory add on (see edit in the question). But it did create a file in / folder! The job ran intermittently it seems as it does not have all the entries I expected. I will dig around and come back! – NewGuyInJava Jul 30 '17 at 04:55
  • I have switched from python to nodejs but have the same issue going on. in crontab I have entry for node /full/path/nodescript.js (note no SUDO). in syslog I do see it runs but this nodescript is not able to run the command ( using child_process.exec) . I am assuming permission error but not really sure where to do. I am very new to linux and permissions are driving me nuts ! – NewGuyInJava Aug 06 '17 at 05:47
  • @NewGuyInJava sorry, I have no idea about nodejs. You should ask a new question for that. – Steffen Winkler Aug 06 '17 at 10:42
  • Nevermind! It was a small mistake in the code on my part. When calling the exce command I was not passing the full path to the file and since the cron runs out of root folder(I assume) it was not able to get to the file. Thanks for your help on this ! – NewGuyInJava Aug 07 '17 at 13:43