# Running a script from cron -\> nothing; running same script from command line =\> SUCCESS ???

**URL:** <https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392>\
**Category:** Ansible Project\
**Created:** [August 31, 2023, 2:24pm UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392 "2023-08-31T14:24:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WyoMurf](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wyomurf/32/3588_2.png) [@WyoMurf](https://forum.ansible.com/u/WyoMurf)\
**Post date:** [August 31, 2023, 2:24pm UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392/1 "2023-08-31T14:24:45Z")

</div>

As the subject line says, I can't get this script to run correctly from cron.

My cron entry (in a file in /etc/cron.d):

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [August 31, 2023, 2:59pm UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392/2 "2023-08-31T14:59:28Z")

</div>

This isn’t really an Ansible problem per se unless it’s _only_ ansible in a subshell that doesn’t run from cron? Have you tested basic cron functionality using something simple like this (or something even more basic):

#!/bin/sh  
z4=`date +%Y%m%d%H%M`  
touch /tmp/${z4}

And then check to see if that file is created every 30 minutes _does_ work?

First questions that spring to mind are:

- is there anything in the cron log, syslog or journal for the crond service?

- is anything else running successfully out of /etc/cron.d / cron.daily etc?

- is crond enabled in whatever init system you’re using? (You haven’t stated which distro?)

---

<div class="post-metadata">

**Author:** ![WyoMurf](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wyomurf/32/3588_2.png) [@WyoMurf](https://forum.ansible.com/u/WyoMurf)\
**Post date:** [August 31, 2023, 5:57pm UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392/3 "2023-08-31T17:57:47Z")

</div>

See below.

> This isn’t really an Ansible problem per se unless it’s _only_ ansible in a subshell that doesn’t run from cron? Have you tested basic cron functionality using something simple like this (or something even more basic):
> 
> #!/bin/sh  
> z4=`date +%Y%m%d%H%M`  
> touch /tmp/${z4}
> 
> And then check to see if that file is created every 30 minutes _does_ work?

Yes, it does. I know the script is running, because it is much elaborate than this, creating files, logging, etc.  
It’s run is annotated in /var/log/cron. If there was an obvious error, like no execute permission, I’d see something in a CMDOUT () message.

No errors.  
I look at the /var/log/ansible.log, and I see the output I’d like to have, in the ansible.log. They are just not being returned  
to the script. It’s as if it’s a “term” sort of problem. So, in the ansible.log, I see something like:

2023-08-29 17:38:18,067 p=84629 u=root n=ansible | mainmachine | CHANGED | rc=0 \>\>  
10.10.11.0##255.255.255.0  
101.133.145.94##255.255.255.255  
.  
.  
.  
and mayhaps a hundred or so more entries.

> First questions that spring to mind are:
> 
> - is there anything in the cron log, syslog or journal for the crond service?

yes. /var/log/cron records it run. ansible.log shows that ansible is called and generates the desired data. It’s not being echoed to the stdout of the ansible process, it appears.

> - is anything else running successfully out of /etc/cron.d / cron.daily etc?

Yep. All else seems well with the cron.d stuff.

> - is crond enabled in whatever init system you’re using? (You haven’t stated which distro?)

I’m running this cron on a AlmaLinux release 8.8 (Sapphire Caracal) OS.

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [August 31, 2023, 7:47pm UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392/4 "2023-08-31T19:47:18Z")

</div>

OK, that’s some useful extra info. I’ve just had a very quick basic play (I haven’t touched cron in earnest for a long time).

Have you tried echo-ing $z4 and appended to a tmp file to see if there’s anything in it at run time?

I’ve tried a few variations in a standard user’s crontab:

root@DESKTOP-9HGJE25:~# crontab -u wmcdonald -l | grep -v ^#

- 
  - 
    - 
      - 
        - ansible -m setup localhost

- 
  - 
    - 
      - 
        - ~/ansible-test

root@DESKTOP-9HGJE25:~# cat /home/wmcdonald/ansible-test  
#!/bin/sh

ansible -m setup localhost

BASIC\_VAR=`ansible -m setup localhost`  
echo $BASIC\_VAR

VERBOSE\_VAR=`ansible -vvv -m ping localhost`  
echo $VERBOSE\_VAR

FILE\_OUTPUT=`ansible -m ping localhost`  
echo $FILE\_OUTPUT \>\> /tmp/ansible.out

The output from the last set of steps shows:

root@DESKTOP-9HGJE25:~# cat /tmp/ansible.out  
localhost | SUCCESS =\> { “changed”: false, “ping”: “pong” }  
localhost | SUCCESS =\> { “changed”: false, “ping”: “pong” }

I did notice from syslog that without an MTA, cron (on Ubuntu, at least) is throwing away output:

/var/log/syslog:Aug 31 20:09:02 DESKTOP-9HGJE25 CRON[2361]: (CRON) info (No MTA installed, discarding output)

---

<div class="post-metadata">

**Author:** ![WyoMurf](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wyomurf/32/3588_2.png) [@WyoMurf](https://forum.ansible.com/u/WyoMurf)\
**Post date:** [September 1, 2023, 1:18am UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392/5 "2023-09-01T01:18:10Z")

</div>

See below for inline answers:

> OK, that’s some useful extra info. I’ve just had a very quick basic play (I haven’t touched cron in earnest for a long time).
> 
> Have you tried echo-ing $z4 and appended to a tmp file to see if there’s anything in it at run time?

Yes, I have, and $z4 is indeed quite empty.

> I’ve tried a few variations in a standard user’s crontab:
> 
> root@DESKTOP-9HGJE25:~# crontab -u wmcdonald -l | grep -v ^#
> 
> - 
> - 
> - 
> - 
> - ansible -m setup localhost
> 
> - 
> - 
> - 
> - 
> - ~/ansible-test
> 
> root@DESKTOP-9HGJE25:~# cat /home/wmcdonald/ansible-test  
> #!/bin/sh
> 
> ansible -m setup localhost
> 
> BASIC\_VAR=`ansible -m setup localhost`  
> echo $BASIC\_VAR
> 
> VERBOSE\_VAR=`ansible -vvv -m ping localhost`  
> echo $VERBOSE\_VAR
> 
> FILE\_OUTPUT=`ansible -m ping localhost`  
> echo $FILE\_OUTPUT \>\> /tmp/ansible.out
> 
> The output from the last set of steps shows:
> 
> root@DESKTOP-9HGJE25:~# cat /tmp/ansible.out  
> localhost | SUCCESS =\> { “changed”: false, “ping”: “pong” }  
> localhost | SUCCESS =\> { “changed”: false, “ping”: “pong” }
> 
> I did notice from syslog that without an MTA, cron (on Ubuntu, at least) is throwing away output:
> 
> /var/log/syslog:Aug 31 20:09:02 DESKTOP-9HGJE25 CRON[2361]: (CRON) info (No MTA installed, discarding output)

Not on this system. It seems clear that the ansible runs, the output is logged, but not sent to the “terminal” because, well,  
I don’t know, but evidently, there’s no “terminal” with cron originating the execution of the shell, perhaps? Any options to ansible for this? Any options in crontab?

---

<div class="post-metadata">

**Author:** ![WyoMurf](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wyomurf/32/3588_2.png) [@WyoMurf](https://forum.ansible.com/u/WyoMurf)\
**Post date:** [September 1, 2023, 5:22am UTC](https://forum.ansible.com/t/running-a-script-from-cron-nothing-running-same-script-from-command-line-success/38392/6 "2023-09-01T05:22:19Z")

</div>

Ah-ha!!! Found it: I had to modify the script, and turn the z4=`ansible ... 2>&1 | ...` into:

/usr/local/bin/ansible mainmachine -m shell -a “psql …” | grep ‘^ [0-9]’ \> /tmp/z4a  
z4=`cat /tmp/z4a`

So, much to my surprise and dismay, the application of 2\>&1 to ansible inside backticks is highly caustic. And, it didn’t really matter that I didn’t use the 2\>&1, as no stderr messages were generated anyway. I am not proficient enough in ansible, nor in the finer points of bash, to render any judgment or condemnation here. So, if anyone else has the same problem, maybe 2 every 10 years, try to remove any filepointer redirections!

Thanks to Will McDonald, for his experiments and help! I really appreciate it!
