1

I'm trying to debug why my cron job is not working

Here's what I have so far:

* * * * * /bin/echo "cron works" >> /tmp/test_cron_hello <-- WORKS
* * * * * cd ~/bot-v2 & date >> /tmp/test_cron_hello <-- WORKS
* * * * * cd ~/bot-v2 & date >> logs/cron.txt <-- DOES NOT WORK

I know that ~/bot-v2/logs/cron.txt is a valid file and folder and I am running crontab -e as root.

Dave
  • 125
  • 4
  • Btw: `>>` is not pipe but [I/O Redirection](https://www.tldp.org/LDP/abs/html/io-redirection.html), This is a pipe: `command | other_command`. – pLumo May 09 '19 at 06:35

1 Answers1

2

You need && instead of &.


Explanation:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. (man bash)

& will send the command (cd ~/bot-v2) to background and it will run in a subshell, so for the next command date >> relative/path/to/file you are not changing directories at all.

Try cd ~/bot-v2 & in terminal and you will see that your current shell won't change directories.


See also:

pLumo
  • 26,204
  • 2
  • 57
  • 87