How to run git pull from subprocess

How to run git pull from subprocess



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


git pull



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


/home/ubuntu/Ingest



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


id_rsa


/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:


cron


id_rsa


# sudo crontab -e
*/1 * * * * STAGE=production /home/ubuntu/Ingest/ingest/movies_ec2.py > /home/ubuntu/test.log 2>&1





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 at 20:42



ubuntu





also don't use shlex.split, just pass the arguments as a list, it's simpler.
– Jean-François Fabre
Aug 28 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 at 21:00


root


sudo crontab -e





not sure, not a linux expert, but you can create a crontab running with a specific user, see stackoverflow.com/questions/8475694/…
– Jean-François Fabre
Aug 28 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 at 21:40



HOME=/home/ubuntu




1 Answer
1



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





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 at 21:13


crontab -e


sudo crontab -e





@MGP -- thanks, yes that's what I'm trying now.
– David542
Aug 28 at 21:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)