0

The following script is running git pull and then write the status to file.

#!/bin/bash
git pull

git log -7 > /var/www/domain.com/v.txt 

When I run the file manually, the file v.txt is created and the content inside as expected.
bash /var/www/domain.io/update.sh

When I run it via cron the file is created but the content is empty
* * * * * bash /var/www/domain.io/update.sh

Whats wrong?

codeforester
  • 39,467
  • 16
  • 112
  • 140
SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • Does this help: https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables –  Feb 20 '18 at 20:40
  • @N3i1 the problem is not with the path, it is with the content. the file is created in the right place. its empty when i run it via cron. – SexyMF Feb 20 '18 at 20:42
  • Do a `type git` or `which git` inside your script and run the cron and see what happens. – codeforester Feb 20 '18 at 20:45
  • @Shazam That's not at all what that question is about. You call the command `git` but because cron isn't picking up your `.profile` and potentially your `PATH` variable, you either have to have it pick up `.profile` or specify the full path to `git` like `/usr/bin/git log -7 > yourfile` Chances are that command is failing because `git` isn't recognized. The file will still be created at 0 bytes and stderr will be routed back to cron. – JNevill Feb 20 '18 at 20:46
  • What is your current directory when you run this manually? What do you think the current directory is when cron runs it? – glenn jackman Feb 20 '18 at 20:46
  • Most likely it's executed with the wrong current working directory. You should be in a git checkout for `git log` to find anything. – Bluehorn Feb 20 '18 at 20:49
  • See this post about managing cron logs: https://stackoverflow.com/questions/41755437/managing-log-files-created-by-cron-jobs. If you had the logs going into a file, you could have noticed your issue much faster. – codeforester Feb 20 '18 at 20:53
  • `cron` sends any output of the command to the user's email. Have you checked the email of the user running the cron job? – Barmar Feb 20 '18 at 21:04
  • Try `PATH=/usr/local/bin:/usr/bin'; cd /; bash /var/www/domain.io/update.sh` and see what happens. – Walter A Feb 20 '18 at 21:17

1 Answers1

0

If I remember correctly the runtime environment of the interactive shell vs cron are different. Specifically the setting of environment variables like PATH can be different. This can effect how scripts run via cron.

As a test you could try the following. Create a small bash shell script that dumps your environment to a file and run this script from both the command line and cron and look for differences.

Eg. file named: r1.sh

#! /bin/bash 
env

From the command line:

$ r1.sh > cmd-line.out

From cron

* * * * * /home/your-user-name/r1.sh > /home/your-user-name/cron.out

NOTE: use full path names for shell script and the output file.

When I did this test on my system, there where a number of difference between the two, for example the PATH variable was much shorter.

If this is the case on your system maybe "git" is not available in the cron env.

Anyway, just an idea to try. I hope this helps.