# git clone is not working in Cron jobs

**URL:** <https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338>\
**Category:** Ansible Project\
**Created:** [April 27, 2017, 2:01pm UTC](https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338 "2017-04-27T14:01:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![balubhai555](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@balubhai555](https://forum.ansible.com/u/balubhai555)\
**Post date:** [April 27, 2017, 2:01pm UTC](https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338/1 "2017-04-27T14:01:34Z")

</div>

Hello Team,

I want to clone the git repository using ansible Cron module. I am trying to change the local repository for every 5 mintes using Cron module in ansible playbook. For that i wrote the code as below.

**The below cron job is not working for git clone**.

- cron:  
name: “download the git folder using cron jobs”  
minute: “5”  
job: ‘sudo “git clone --depth=1 --force=yes [https://github.com/Msutapalli/openwhisk.git](https://github.com/Msutapalli/openwhisk.git)” \> /home/ec2-user/siva/openw/’  
cron\_file: “/etc/crontab”  
state: present  
user: ec2-user  
user: root

But for sample

- cron:  
name: “Sample hello”  
minute: “5”  
job: ‘sudo echo " heloworld " \> /home/ec2-user/hello.txt’  
cron\_file: “/etc/crontab”  
state: present  
user: ec2-user  
user: root

It is working fine.

where i did wrong in my **'job: ’** command.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 27, 2017, 3:44pm UTC](https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338/2 "2017-04-27T15:44:21Z")

</div>

a few issues:

- ‘user’ is defined twice?
- you are sudo’ing from a cronjob - why not manipulate the target user’s cronjob directly?
- the output of git is redirected to a directory, I guess you either meant to clone into this directory, or redirect the output to a file (output != repository location)

Dick

---

<div class="post-metadata">

**Author:** ![Sander\_Smeenk](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sander\_Smeenk](https://forum.ansible.com/u/Sander_Smeenk)\
**Post date:** [April 27, 2017, 3:58pm UTC](https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338/3 "2017-04-27T15:58:35Z")

</div>

Quoting balubhai555 (balubhai555@gmail.com):

> I want to clone the git repository using ansible Cron module.

So why not create a playbook that does this and run it from cron? 😉

> \*The below cron job is not working for git clone\*.  
> job: 'sudo "git clone --depth=1 --force=yes [https://github.com/Msutapalli/openwhisk.git&quot](https://github.com/Msutapalli/openwhisk.git&quot); \> /home/ec2-user/siva/openw/'

Drop the double quotes.  
Also, a filename seems to be missing in your stdout redirection.

$ sudo "git help"  
sudo: git help: command not found

$ sudo git help  
usage: git [--version] [--help] [-C \<path\>] [-c name=value]  
[..]

> But for sample  
> job: 'sudo echo " heloworld " \> /home/ec2-user/hello.txt'  
> It is working fine.

Notice 'echo' is not in the double quotes.

-Sndr.

---

<div class="post-metadata">

**Author:** ![balubhai555](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@balubhai555](https://forum.ansible.com/u/balubhai555)\
**Post date:** [April 27, 2017, 5:33pm UTC](https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338/4 "2017-04-27T17:33:44Z")

</div>

Yes Dick, You are right.

1.) ‘user’ defined twice because my intention is to use the cron job for both users.  
2.) Yes, I tried without sudo, But no effect with sudo or without sudo’ing in the clone.  
3.) Yes, you are right. I am trying to store the repository into those folder.

Can you suggest me more changes to work with cron with git clone.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 27, 2017, 6:23pm UTC](https://forum.ansible.com/t/git-clone-is-not-working-in-cron-jobs/24338/5 "2017-04-27T18:23:43Z")

</div>

> Yes Dick, You are right.
> 
> 1.) ‘user’ defined twice because my intention is to use the cron job for both users.

That’s unlikely to work. If you actually do want that, I suggest iterating over a list of users using ‘with\_items’.  
(If it does work then it needs documentation.)

> 2.) Yes, I tried without sudo, But no effect with sudo or without sudo’ing in the clone.

Don’t use it as it complicates things for no reason.

> 3.) Yes, you are right. I am trying to store the repository into those folder.
> 
> Can you suggest me more changes to work with cron with git clone.
> 
> >

cron schedules commands, and the ansible cron module is a convenient way to configure it (timing, user, etc).

The problem is that your command doesn’t work.  
Scheduling something that doesn’t work has the same result: it still doesn’t work.  
So fix your command first, and once it works you can try to schedule it.

Hint: make sure you are able to clone a git repository into a specific directory: [https://git-scm.com/docs/git-clone](https://git-scm.com/docs/git-clone)

I’m not sure if repeatedly cloning a git repo is actually useful, so make sure you understand what the implications are of doing that (or come up with a different approach).

Dick
