0

I have an executable bash script (.sh) which should be run 2 python scripts consistently (only when first is successfully finished then the second one will be started), run_script.sh has the following view:

#!/bin/bash
#!/usr/bin/python3
scripts='/path/to/file/file1.py /path/to/file/file2.py'
for s in $scripts
do
    echo $s
    /usr/bin/python3 $s
done

As a scheduler I use crontab job:

26 14 10-23 * * cd /path/to/file/ && /path/to/file/run_script.sh >> /home/log/crontab_LOG.log 2>&1

In process of data aggregation in python scripts I use .pickle files. But, in result of execution the suggested python scripts (file1.py and file2.py) by .sh file, I received and error:

PermissionError [Errno 13 ] Permission denied: '/path/to/file/example.pickle'

btw: example.pickle file is created in process of execute file1.py,not out of run where it used.

When try to execute sh run_script.sh by terminal without any scheduler, there is no any error to permissions. So, result is success.

I also try to search my question, but it was not success. (Permission denied for Python script using Bash?), IO Error while storing data in pickle

The question is how I can set the the root permission by bash script to required python scripts?

Cindy
  • 568
  • 7
  • 20
  • 1
    The problem is not the execute permission of the bash script. the problem is that your python script is running as a user that does not have write permission to `path/to/file/example.pickle`. Perhaps that is because `path/to/file/example.pickle` is missing the `/` character at the beginning? – Andrew Vickers Oct 23 '19 at 15:08
  • 1
    is your `run_script.sh` and `file1.py` are in the same directory well as `.pickle` file. Basically check if all these scripts have the proper flow of directories. Also, check if `cron` user have permission for these scripts if not running from `root` – Avinash Yadav Oct 23 '19 at 15:09
  • @AvinashYadav, now `run_script.sh` and `file1.py` are in the same directory, but the path for saved by `file1.py` script example.pickle file is in subdirectory of folder where `file1.py` located. I'll try to move it to one directory. Yes, cron user has permission to run scripts. Thanks for idea. – Cindy Oct 23 '19 at 16:18
  • @AndrewVickers, it has '/' at the beginning, it was just my typos in question. Thanks – Cindy Oct 23 '19 at 16:19
  • @Cindy Did you tried moving in the same directory – Avinash Yadav Oct 24 '19 at 08:01
  • @AvinashYadav, yes I moved it to the same directory and modified the `crontab job` to the following view: `30 14 10-23 * * cd /home/path/to/file/ && /usr/bin/python3 file1.py && /usr/bin/python3 file2.py >> /home/logs/crontab_LOG.log 2>&1`. Now It works, but I need to find solution how to move results file (`.pickle`) from main directory (where `file1.py` and `file2.py` are located). I tried to move it to subdirectory, but an error about `Permission denied` happens again. – Cindy Oct 24 '19 at 08:38
  • @Cindy Good to know that it worked, I can help is you share your file1.py snippet where it try to access .pickle file. – Avinash Yadav Oct 24 '19 at 10:30
  • @AvinashYadav, there is a function in `file1.py`: `def readPickleData(key, path, file='example.pickle'):
 data = pickle.load(open(path + file, 'rb'))
 print(data.keys())
 data = data[key]
 return data`.So, in process of run `file1.py` I save `df` to `example.pickle` then use it for next aggregation. Code for saving `df` to `pickle` is here:`pc.dump({'data': df, },
 open(path+'example.pickle`. It would be great to move `example.pickle` from main directory (where `file1.py` located: `main_code`) to subdirectory (`/main_code/files`) with permissions to it. How it could be achieve? – Cindy Oct 24 '19 at 11:12

1 Answers1

0

It was fixed by usage approach with Path:

from pathlib import Path
example_pickle = Path('logs/example_pickle'+datetime.now()+'.pickle')

Thanks everyone for the inspired ideas:)

Cindy
  • 568
  • 7
  • 20