0

I am trying to run a subprocess command to do a git pull.

The cwd of the Git repository is /home/ubuntu/Ingest.

The id_rsa that I'm using with Github is located at /home/ubuntu/.ssh/id_rsa.

How would I run a subprocess call to do the following?

import shlex, subprocess
subprocess.call(shlex.split('git pull origin master'), cwd='/home/ubuntu/Ingest')

The log looks like:

movies_ec2.py:43@__init__ [INFO] Version not up to date...Doing a git pull and exiting...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

The script is running from cron and is not picking up on the id_rsa. (Note: I am not looking to use GitPython). What do I need to change in my cron job or script so that this will work? My cron job is currently:

# sudo crontab -e
*/1 * * * * STAGE=production /home/ubuntu/Ingest/ingest/movies_ec2.py > /home/ubuntu/test.log 2>&1
David542
  • 104,438
  • 178
  • 489
  • 842
  • try to check current user when running as cron: cron is run by a different user, maybe not `ubuntu`. so permissions issue... – Jean-François Fabre Aug 28 '18 at 20:42
  • also don't use shlex.split, just pass the arguments as a list, it's simpler. – Jean-François Fabre Aug 28 '18 at 20:42
  • @Jean-FrançoisFabre it seems to be running as the `root` user fron `sudo crontab -e`. What should I change this to? – David542 Aug 28 '18 at 21:00
  • not sure, not a linux expert, but you can create a crontab running with a specific user, see https://stackoverflow.com/questions/8475694/how-to-specify-in-crontab-by-what-user-to-run-script – Jean-François Fabre Aug 28 '18 at 21:01
  • Make sure that `HOME=/home/ubuntu` in the context of your cron job. (Towards this end, make sure you're putting this in the ubuntu user's crontab, not root's). – Charles Duffy Aug 28 '18 at 21:40
  • BTW, it's better form to hardcode the list -- `['git', 'pull', 'origin', 'master']`, without the `shlex.split()` involvement. You'll be glad you went that route when you need to be able to substitute in values that can contain spaces without needing to worry about shell-equivalent escaping first. – Charles Duffy Aug 28 '18 at 21:42

1 Answers1

0

The following answer addresses this question quite well: How to specify in crontab by what user to run script?. However, in short, this can be accomplished by modifying the user in the crontab to be "ubuntu", which is the current user that is used to create all the files, etc.

$ sudo vim /etc/crontab
*/1 * * * *     ubuntu   /home/ubuntu/Ingest/ingest/movies_ec2.py > /home/ubuntu/test.log 2>&1
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    Also, you could've used the crontab from the ubuntu user itself. (`crontab -e` when logged in as ubuntu) With `sudo crontab -e` you were writing to root's crontab. – MGP Aug 28 '18 at 21:13